it compiled

This commit is contained in:
2022-06-08 09:07:09 -05:00
parent 1884c2b8e9
commit ffe6969889
12 changed files with 996 additions and 1130 deletions

View File

@@ -1,4 +1,4 @@
import { build } from 'esbuild'
import { build, BuildFailure } from 'esbuild'
import {
lstatSync,
readdirSync,
@@ -7,39 +7,51 @@ import {
writeFileSync,
} from 'fs'
import { DistBase, DistPath, ScriptBase, ScriptPath } from './paths'
import { readmeData, updateReadmeFile } from './readmefile'
import readMeta from './readmeta'
import { UserScriptMetaFull } from './types'
async function compileProject(name: string) {
async function compileProject(
name: string
): Promise<[UserScriptMetaFull, null | string]> {
//read meta file
let [metaJson, metaString] = readMeta(name)
let outPath = DistPath(name)
await build({
entryPoints: [ScriptPath(name).main],
outfile: outPath,
let error: null | string = null
target: 'esnext',
platform: 'node',
format: 'esm',
try {
await build({
entryPoints: [ScriptPath(name).main],
outfile: outPath,
bundle: true,
minify: false,
target: 'esnext',
platform: 'node',
format: 'esm',
define: {
UserScriptName: `'${metaJson.name}'`,
UserScriptNamespace: `'${metaJson.namespace}'`,
UserScriptVersion: `'${metaJson.version}'`,
bundle: true,
minify: false,
UserScriptDownloadURL: `'${metaJson.downloadURL}'`,
UserScriptSupportURL: `'${metaJson.supportURL}'`,
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
},
})
define: {
UserScriptName: `'${metaJson.name}'`,
UserScriptNamespace: `'${metaJson.namespace}'`,
UserScriptVersion: `'${metaJson.version}'`,
UserScriptDownloadURL: `'${metaJson.downloadURL}'`,
UserScriptSupportURL: `'${metaJson.supportURL}'`,
UserScriptHomepageURL: `'${metaJson.homepageURL}'`,
},
})
} catch (e) {
error = (e as BuildFailure).message
}
//add UserScript header
let content = readFileSync(outPath).toString()
writeFileSync(outPath, metaString + content)
return [metaJson, error]
}
if (!lstatSync('package.json').isFile()) {
@@ -51,14 +63,29 @@ if (!lstatSync('package.json').isFile()) {
readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
//compile scripts
readdirSync(ScriptBase).forEach(name => {
let path = ScriptPath(name)
;(async () => {
let scripts = readdirSync(ScriptBase)
// let scriptMeta: {
// [name: string]: [UserScriptMetaFull, string | null]
// } = {}
if (
!name.endsWith('_') &&
lstatSync(path.dir).isDirectory() &&
lstatSync(path.main).isFile()
) {
compileProject(name)
let scriptMeta: readmeData[] = []
for (let name of scripts) {
let path = ScriptPath(name)
if (
!name.endsWith('_') &&
lstatSync(path.dir).isDirectory() &&
lstatSync(path.main).isFile()
) {
let [meta, error] = await compileProject(name)
scriptMeta.push({
meta,
error,
})
}
}
})
updateReadmeFile(scriptMeta)
})()

61
src/readmefile.ts Normal file
View File

@@ -0,0 +1,61 @@
import { readdirSync, readFileSync, writeFileSync } from 'fs'
import { UserScriptMetaFull } from './types'
export interface readmeData {
meta: UserScriptMetaFull
error: string | null
}
export function updateReadmeFile(fileList: readmeData[]) {
let readmeFile = getReadmeFileName()
if (readmeFile !== null) {
let [readmeStart, readmeEnd] = readReadmeFile(readmeFile)
let installLinks = fileList.map(readmeDataToString).join('\n')
let installLinksAll = `
<!-- START INSTALL LINKS -->
## Installs
${installLinks}
<!-- END INSTALL LINKS -->
`
let content = [readmeStart, installLinksAll, readmeEnd].join('\n')
writeFileSync(readmeFile, content)
}
}
function readmeDataToString(readmeData: readmeData): string {
let { meta, error } = readmeData
let errStr = error !== null ? '~~' : ''
let errMsg = error !== null ? `\n - ${error}` : ''
return `- ${errStr}[${meta.name}](${meta.downloadURL})})${errStr}${errMsg}`
}
function getReadmeFileName(): string | null {
let files = readdirSync('.')
for (let name of files) {
if (/^readme\.md$/i.test(name)) {
return name
}
}
return null
}
function readReadmeFile(readmeFile: string): [string, string] {
let content = readFileSync(readmeFile).toString()
const regex =
/<!-- START INSTALL LINKS -->(?:.|\n)*?<!-- END INSTALL LINKS -->/
const index = regex.exec(content)?.index
if (index === undefined) {
if (!content.endsWith('\n')) {
content += '\n'
}
return [content, '']
}
content = content.replace(regex, '')
return [content.slice(0, index), content.slice(index)]
}

View File

@@ -1,9 +1,13 @@
import { lstatSync, readFileSync } from 'fs'
import { BaseUrl, FileUrl, ScriptPath, SupportUrl } from './paths'
import { UserDataMeta, UserDataMetaFull, UserDataMetaPartial } from './types'
import {
UserScriptMeta,
UserScriptMetaFull,
UserScriptMetaPartial,
} from './types'
export default function (name: string): [UserDataMetaFull, string] {
var meta: UserDataMetaFull = {
export default function (name: string): [UserScriptMetaFull, string] {
var meta: UserScriptMetaFull = {
name: name,
namespace: 'zomo.dev',
match: '',
@@ -27,11 +31,11 @@ export default function (name: string): [UserDataMetaFull, string] {
if (lstatSync(metaPath).isFile()) {
try {
let args: UserDataMetaPartial = JSON.parse(
let args: UserScriptMetaPartial = JSON.parse(
readFileSync(metaPath).toString()
)
let key: keyof UserDataMeta
let key: keyof UserScriptMeta
for (key in meta) {
let val = args[key]
@@ -59,7 +63,7 @@ export default function (name: string): [UserDataMetaFull, string] {
return [
meta,
`// ==UserScript==
${(Object.keys(meta) as Array<keyof UserDataMetaFull>)
${(Object.keys(meta) as Array<keyof UserScriptMetaFull>)
.filter(key => {
let val = meta[key]
if (val === undefined) return false
@@ -69,7 +73,7 @@ ${(Object.keys(meta) as Array<keyof UserDataMetaFull>)
})
.map(key => {
let val = meta[key]
let key_str =
let key_str: string =
key in keyConversion
? keyConversion[key as keyof typeof keyConversion]
: key

View File

@@ -1,4 +1,4 @@
export interface UserDataMetaPartial {
export interface UserScriptMetaPartial {
name?: string
namespace?: string
match?: string | string[]
@@ -18,7 +18,7 @@ export interface UserDataMetaPartial {
unwrap?: boolean
}
export interface UserDataMeta extends UserDataMetaPartial {
export interface UserScriptMeta extends UserScriptMetaPartial {
name: string
namespace: string
version: string
@@ -27,7 +27,7 @@ export interface UserDataMeta extends UserDataMetaPartial {
homepageURL: string
}
export interface UserDataMetaFull extends UserDataMeta {
export interface UserScriptMetaFull extends UserScriptMeta {
match: string | string[]
excludematch: string | string[]
description: string