mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2025-12-11 08:46:16 +00:00
114 lines
2.5 KiB
JavaScript
114 lines
2.5 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import {glob} from "glob";
|
|
import {readFile, writeFile} from "fs/promises"
|
|
|
|
const options = {
|
|
format: {
|
|
override: {
|
|
scripts: {
|
|
"format": "prettier .",
|
|
"format:fix": "prettier --write ."
|
|
},
|
|
prettier: "@openstapps/prettier-config"
|
|
},
|
|
if: "@openstapps/prettier-config"
|
|
},
|
|
lint: {
|
|
override: {
|
|
scripts: {
|
|
"lint": "eslint --ext .ts src/",
|
|
"lint:fix": "eslint --fix --ext .ts src/"
|
|
},
|
|
eslintConfig: {
|
|
extends: ["@openstapps"]
|
|
}
|
|
},
|
|
if: "@openstapps/eslint-config"
|
|
},
|
|
build: {
|
|
override: {
|
|
scripts: {
|
|
build: "tsup --dts",
|
|
},
|
|
tsup: {
|
|
entry: ["src/index.ts"],
|
|
sourcemap: true,
|
|
clean: true,
|
|
format: "esm",
|
|
outDir: "lib"
|
|
},
|
|
},
|
|
if: "tsup"
|
|
},
|
|
test: {
|
|
override: {
|
|
scripts: {
|
|
"test": "nyc mocha 'test/**/*.spec.ts'"
|
|
},
|
|
nyc: {
|
|
extends: "@openstapps/nyc-config"
|
|
}
|
|
},
|
|
if: "@openstapps/nyc-config"
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param path {string}
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async function fixPackage(path) {
|
|
const file = JSON.parse(await readFile(path, 'utf-8'))
|
|
console.log(path)
|
|
/** @type {string[]} */
|
|
const overrides = file['openstapps-configuration']?.['overrides'] || []
|
|
/** @type {string[]} */
|
|
const dependencies = Object.keys(file['devDependencies'] || {})
|
|
|
|
for (const option in options) {
|
|
if (overrides.includes(option)) continue
|
|
|
|
const condition = options[option].if
|
|
const override = options[option].override
|
|
|
|
if (!dependencies.includes(condition)) continue
|
|
if (option === 'build') {
|
|
const bin = file['bin']
|
|
const entries = []
|
|
if (typeof bin === 'string') {
|
|
entries.push(bin)
|
|
} else if (typeof bin === 'object') {
|
|
for (const key in bin) {
|
|
entries.push(bin[key])
|
|
}
|
|
}
|
|
const main = file['main']
|
|
if (main) {
|
|
entries.push(main)
|
|
}
|
|
|
|
override['tsup']['entry'] = entries.map(it => it.replace(/^((\.\/)?lib\/)?/, 'src/').replace(/\.js$/, '.ts'))
|
|
}
|
|
|
|
for (const key in override) {
|
|
if (key === 'scripts') {
|
|
for (const script in override[key]) {
|
|
file[key][script] = override[key][script]
|
|
}
|
|
} else {
|
|
file[key] = override[key]
|
|
}
|
|
}
|
|
}
|
|
|
|
await writeFile(path, JSON.stringify(file, null, 2))
|
|
}
|
|
|
|
async function run() {
|
|
const packages = await glob('*/**/package.json')
|
|
await Promise.all(packages.map(fixPackage))
|
|
}
|
|
|
|
await run()
|