refactord paths.ts
This commit is contained in:
31
src/build.ts
31
src/build.ts
@@ -1,10 +1,9 @@
|
||||
import { buildSync, BuildFailure, BuildOptions, BuildResult } from 'esbuild'
|
||||
import { existsSync, writeFileSync, unlinkSync } from 'fs'
|
||||
import { DistPath, ScriptBase, ScriptPath } from './paths'
|
||||
import { UserScriptMetaFull } from './types'
|
||||
import readMeta from './readmeta'
|
||||
import { format, resolveConfig } from 'prettier'
|
||||
import { CLIArgs } from './main'
|
||||
import { AllPaths, CLIArgs } from './main'
|
||||
|
||||
export default interface runBuild {
|
||||
meta: UserScriptMetaFull
|
||||
@@ -17,12 +16,12 @@ export default function runBuild(
|
||||
) {
|
||||
//read meta file
|
||||
let { meta, metaString } = readMeta(name)
|
||||
let pathDist = DistPath(name)
|
||||
let paths = AllPaths.script(name)
|
||||
|
||||
let result = runEsbuild(
|
||||
{
|
||||
entryPoints: [ScriptPath(name).main],
|
||||
outfile: pathDist,
|
||||
entryPoints: [paths.main],
|
||||
outfile: paths.dist,
|
||||
|
||||
target: 'esnext',
|
||||
platform: 'node',
|
||||
@@ -117,16 +116,15 @@ function getResult(error: BuildFailure | null, result: BuildResult | null) {
|
||||
}
|
||||
|
||||
function clearFilenameComments(content: string): string {
|
||||
let regexp = new RegExp(`//\\s*${ScriptBase}/.*(?:\\n|$)`, 'g')
|
||||
let regexp = new RegExp(`//\\s*${AllPaths.base.script}/.*(?:\\n|$)`, 'g')
|
||||
return content.replace(regexp, '')
|
||||
}
|
||||
|
||||
function postBuild(name: string, result: RunEsbuildResult, metaString: string) {
|
||||
let error: string | null = null
|
||||
let path = ScriptPath(name)
|
||||
let pathDist = DistPath(name)
|
||||
let paths = AllPaths.script(name)
|
||||
|
||||
let PrettierConfig = resolveConfig.sync(path.dir) || {}
|
||||
let PrettierConfig = resolveConfig.sync(paths.dir) || {}
|
||||
|
||||
if (result.error) {
|
||||
console.error(name, result.errorRaw || result.error)
|
||||
@@ -139,7 +137,7 @@ function postBuild(name: string, result: RunEsbuildResult, metaString: string) {
|
||||
parser: 'babel',
|
||||
})
|
||||
}
|
||||
writeFileSync(pathDist, content)
|
||||
writeFileSync(paths.dist, content)
|
||||
} else {
|
||||
console.error(name, 'No output')
|
||||
}
|
||||
@@ -150,17 +148,16 @@ function postBuild(name: string, result: RunEsbuildResult, metaString: string) {
|
||||
}
|
||||
|
||||
function doErrorFile(name: string, error: string | null) {
|
||||
let path = ScriptPath(name)
|
||||
let outfile = DistPath(name)
|
||||
let paths = AllPaths.script(name)
|
||||
|
||||
let content = `${new Date().toISOString()}\n\n${error}`
|
||||
|
||||
if (error !== null) {
|
||||
writeFileSync(path.error, content)
|
||||
if (existsSync(outfile)) {
|
||||
unlinkSync(outfile)
|
||||
writeFileSync(paths.error, content)
|
||||
if (existsSync(paths.dist)) {
|
||||
unlinkSync(paths.dist)
|
||||
}
|
||||
} else if (existsSync(path.error)) {
|
||||
unlinkSync(path.error)
|
||||
} else if (existsSync(paths.error)) {
|
||||
unlinkSync(paths.error)
|
||||
}
|
||||
}
|
||||
|
||||
33
src/main.ts
33
src/main.ts
@@ -1,16 +1,22 @@
|
||||
import { existsSync, lstatSync, readdirSync, unlinkSync } from 'fs'
|
||||
import commandLineArgs from 'command-line-args'
|
||||
import { DistBase, ScriptBase, ScriptPath } from './paths'
|
||||
import { updateReadmeFile } from './readmefile'
|
||||
import runBuild from './build'
|
||||
import * as Path from 'path'
|
||||
import getAllPaths from './paths'
|
||||
|
||||
export interface CLIArgsT {
|
||||
watch: boolean
|
||||
minify: boolean
|
||||
prettier: boolean
|
||||
srccomment: boolean
|
||||
help: boolean
|
||||
|
||||
baseurl?: string
|
||||
remotebranch?: string
|
||||
scriptpath?: string
|
||||
distpath?: string
|
||||
|
||||
help?: boolean
|
||||
}
|
||||
|
||||
export const CLIArgs = commandLineArgs([
|
||||
@@ -18,9 +24,22 @@ export const CLIArgs = commandLineArgs([
|
||||
{ name: 'minify', alias: 'm', type: Boolean, defaultValue: false },
|
||||
{ name: 'prettier', alias: 'p', type: Boolean, defaultValue: false },
|
||||
{ name: 'srccomment', alias: 's', type: Boolean, defaultValue: false },
|
||||
{ name: 'help', alias: 'h', type: Boolean, defaultValue: false },
|
||||
|
||||
{ name: 'baseurl', alias: 'bu', type: String, defaultValue: '' },
|
||||
{ name: 'remotebranch', alias: 'rb', type: String, defaultValue: 'main' },
|
||||
{ name: 'scriptsdir', alias: 'sd', type: String, defaultValue: 'scripts' },
|
||||
{ name: 'distdir', alias: 'dd', type: String, defaultValue: 'dist' },
|
||||
|
||||
{ name: 'help', alias: 'h', type: Boolean },
|
||||
]) as CLIArgsT
|
||||
|
||||
export const AllPaths = getAllPaths({
|
||||
baseURL: CLIArgs.baseurl || '',
|
||||
remoteBranch: CLIArgs.remotebranch || 'main',
|
||||
scriptBase: CLIArgs.scriptpath || 'scripts',
|
||||
distBase: CLIArgs.distpath || 'dist',
|
||||
})
|
||||
|
||||
if (CLIArgs.help) {
|
||||
let command = '<command>'
|
||||
if (process.argv.length > 0) {
|
||||
@@ -61,14 +80,16 @@ if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
|
||||
}
|
||||
|
||||
//delete compiled scripts
|
||||
readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
|
||||
readdirSync(AllPaths.base.dist).forEach(file =>
|
||||
unlinkSync(`${AllPaths.base.dist}/${file}`)
|
||||
)
|
||||
|
||||
//compile scripts
|
||||
let scripts = readdirSync(ScriptBase)
|
||||
let scripts = readdirSync(AllPaths.base.script)
|
||||
let scriptMeta: runBuild[] = []
|
||||
|
||||
for (let name of scripts) {
|
||||
let path = ScriptPath(name)
|
||||
let path = AllPaths.script(name)
|
||||
|
||||
if (
|
||||
!name.endsWith('_') &&
|
||||
|
||||
51
src/paths.ts
51
src/paths.ts
@@ -1,23 +1,42 @@
|
||||
export const BaseUrl = `https://git.zomo.dev/zomo/browser-scripts`
|
||||
export const RemoteBranch = 'main'
|
||||
export const ScriptBase = 'scripts'
|
||||
export const DistBase = 'dist'
|
||||
|
||||
export const SupportUrl = `${BaseUrl}/issues`
|
||||
export const FileUrl = (name: string) =>
|
||||
`${BaseUrl}/raw/branch/${RemoteBranch}/${DistBase}/${name}.user.js`
|
||||
|
||||
export interface ScriptPathT {
|
||||
dir: `${string}/${string}`
|
||||
main: `${string}/${string}/main.ts`
|
||||
meta: `${string}/${string}/meta.json`
|
||||
error: `${string}/${string}/error.log`
|
||||
dist: `${string}/${string}.user.js`
|
||||
url: `${string}/raw/branch/${string}/${string}/${string}.user.js`
|
||||
}
|
||||
export const ScriptPath = (name: string): ScriptPathT => ({
|
||||
dir: `${ScriptBase}/${name}`,
|
||||
main: `${ScriptBase}/${name}/main.ts`,
|
||||
meta: `${ScriptBase}/${name}/meta.json`,
|
||||
error: `${ScriptBase}/${name}/error.log`,
|
||||
})
|
||||
|
||||
export const DistPath = (name: string) => `${DistBase}/${name}.user.js`
|
||||
export interface getAllPathsOps {
|
||||
baseURL: string
|
||||
remoteBranch: string
|
||||
scriptBase: string
|
||||
distBase: string
|
||||
}
|
||||
|
||||
export default function getAllPaths({
|
||||
baseURL,
|
||||
remoteBranch,
|
||||
scriptBase,
|
||||
distBase,
|
||||
}: getAllPathsOps) {
|
||||
return {
|
||||
base: {
|
||||
branch: remoteBranch,
|
||||
script: scriptBase,
|
||||
dist: distBase,
|
||||
},
|
||||
url: {
|
||||
base: baseURL,
|
||||
support: `${baseURL}/issues`,
|
||||
},
|
||||
script: (name: string): ScriptPathT => ({
|
||||
dir: `${scriptBase}/${name}`,
|
||||
main: `${scriptBase}/${name}/main.ts`,
|
||||
meta: `${scriptBase}/${name}/meta.json`,
|
||||
error: `${scriptBase}/${name}/error.log`,
|
||||
dist: `${distBase}/${name}.user.js`,
|
||||
url: `${baseURL}/raw/branch/${remoteBranch}/${distBase}/${name}.user.js`,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { existsSync, lstatSync, readFileSync } from 'fs'
|
||||
import { BaseUrl, FileUrl, ScriptPath, SupportUrl } from './paths'
|
||||
import { AllPaths } from './main'
|
||||
import {
|
||||
UserScriptMeta,
|
||||
UserScriptMetaFull,
|
||||
@@ -12,6 +12,9 @@ export default interface readMeta {
|
||||
}
|
||||
|
||||
export default function readMeta(name: string) {
|
||||
let paths = AllPaths.script(name)
|
||||
let urls = AllPaths.url
|
||||
|
||||
var meta: UserScriptMetaFull = {
|
||||
name: name,
|
||||
namespace: 'zomo.dev',
|
||||
@@ -26,13 +29,13 @@ export default function readMeta(name: string) {
|
||||
noframes: false,
|
||||
grant: '',
|
||||
injectinto: '',
|
||||
downloadURL: FileUrl(name),
|
||||
supportURL: SupportUrl,
|
||||
homepageURL: BaseUrl,
|
||||
downloadURL: paths.url,
|
||||
supportURL: urls.support,
|
||||
homepageURL: urls.base,
|
||||
unwrap: false,
|
||||
}
|
||||
|
||||
let metaPath = ScriptPath(name).meta
|
||||
let metaPath = paths.meta
|
||||
|
||||
if (existsSync(metaPath) && lstatSync(metaPath).isFile()) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user