prettier support

This commit is contained in:
2022-06-09 11:09:22 -05:00
parent 57f7599739
commit e8de01b48a
8 changed files with 36 additions and 33 deletions

View File

@@ -3,12 +3,14 @@ import { existsSync, writeFileSync, unlinkSync } from 'fs'
import { DistPath, ScriptBase, ScriptPath } from './paths'
import { UserScriptMetaFull } from './types'
import readMeta from './readmeta'
import { format, Options } from 'prettier'
export default function (
name: string,
watchCallback:
| ((meta: UserScriptMetaFull, error: string | null) => void)
| false = false
| false = false,
PrettierConfig: Options | null
): [UserScriptMetaFull, string | null] {
//read meta file
let [metaJson, metaString] = readMeta(name)
@@ -44,7 +46,11 @@ export default function (
console.error(name, result.errorRaw || result.error)
error = result.error
} else if (result.content) {
writeFileSync(pathDist, metaString + result.content)
let content = format(
metaString + result.content,
PrettierConfig === null ? undefined : PrettierConfig
)
writeFileSync(pathDist, content)
} else {
console.error(name, 'No output')
}
@@ -64,7 +70,11 @@ export default function (
console.error(name, result.errorRaw || result.error)
error = result.error
} else if (result.content) {
writeFileSync(pathDist, metaString + result.content)
let content = format(
metaString + result.content,
PrettierConfig === null ? undefined : PrettierConfig
)
writeFileSync(pathDist, content)
} else {
console.error(name, 'No output')
}
@@ -160,18 +170,6 @@ function runEsbuild(
}
}
// //only remove the filename comments if there's only 1 file
// function clearFilenameComments(content: string): string {
// let regexp = new RegExp(`//\\s*${ScriptBase}/.*(?:\\n|$)`, 'g')
// let matches = content.match(regexp)
// if (matches) {
// if (matches.length === 1) {
// content = content.replace(regexp, '')
// }
// }
// return content
// }
//remove all filename comments
function clearFilenameComments(content: string): string {
let regexp = new RegExp(`//\\s*${ScriptBase}/.*(?:\\n|$)`, 'g')

View File

@@ -1,5 +1,6 @@
import { existsSync, lstatSync, readdirSync, unlinkSync } from 'fs'
import commandLineArgs from 'command-line-args'
import { resolveConfig } from 'prettier'
import { DistBase, ScriptBase, ScriptPath } from './paths'
import { readmeData, updateReadmeFile } from './readmefile'
import runBuild from './build'
@@ -20,6 +21,8 @@ if (!existsSync('package.json') || !lstatSync('package.json').isFile()) {
//delete compiled scripts
readdirSync(DistBase).forEach(file => unlinkSync(`${DistBase}/${file}`))
const PrettierConfig = resolveConfig.sync(process.cwd())
//compile scripts
let scripts = readdirSync(ScriptBase)
let scriptMeta: readmeData[] = []
@@ -47,7 +50,8 @@ for (let name of scripts) {
let [meta, error] = runBuild(
name,
CLIArgs.watch ? postWatchUpdate : false
CLIArgs.watch ? postWatchUpdate : false,
PrettierConfig
)
scriptMeta[id] = { meta, error }