chore: use esmodule for scripts

This commit is contained in:
Rongjian Zhang 2021-08-01 21:24:04 +08:00
parent f10110c5f9
commit 483a2ab23e
6 changed files with 38 additions and 36 deletions

View File

@ -3,18 +3,18 @@
"private": true,
"scripts": {
"clean": "rm -rf packages/*/{lib,dist,tsconfig.tsbuildinfo}",
"build": "tsc --build && node -r esm scripts/process.js && rollup -c",
"build": "tsc --build && node scripts/process.mjs && rollup -c",
"build:ci": "UMD=1 npm run build",
"dev": "run-p dev:bundle dev:tsc dev:process",
"dev:bundle": "rollup -cw",
"dev:tsc": "tsc --build --watch",
"dev:process": "node -r esm scripts/process.js --watch",
"dev:process": "node scripts/process.mjs --watch",
"test": "BABEL_ENV=test jest",
"test:watch": "BABEL_ENV=test npm run test -- --watch",
"postinstall": "node -r esm scripts/postinstall.js",
"postinstall": "node scripts/postinstall.mjs",
"pub": "npm run clean && npm run build:ci && lerna publish --yes",
"lint": "npx prettier --plugin-search-dir=. --ignore-path=.gitignore --list-different **/*.{ts,tsx,js,svelte}",
"lint:fix": "npx prettier --plugin-search-dir=. --ignore-path=.gitignore --write **/*.{ts,tsx,js,svelte}"
"lint": "prettier --plugin-search-dir=. --ignore-path=.gitignore --list-different **/*.{ts,tsx,js,svelte}",
"lint:fix": "prettier --plugin-search-dir=. --ignore-path=.gitignore --write **/*.{ts,tsx,js,svelte}"
},
"devDependencies": {
"@babel/core": "^7.14.6",
@ -31,7 +31,6 @@
"@testing-library/svelte": "^3.0.3",
"@types/jest": "^26.0.24",
"chokidar": "^3.5.2",
"esm": "^3.2.25",
"fs-extra": "^10.0.0",
"glob": "^7.1.7",
"jest": "^27.0.6",

View File

@ -18,7 +18,6 @@ importers:
'@testing-library/svelte': ^3.0.3
'@types/jest': ^26.0.24
chokidar: ^3.5.2
esm: ^3.2.25
fs-extra: ^10.0.0
glob: ^7.1.7
jest: ^27.0.6
@ -58,7 +57,6 @@ importers:
'@testing-library/svelte': 3.0.3_svelte@3.38.3
'@types/jest': 26.0.24
chokidar: 3.5.2
esm: 3.2.25
fs-extra: 10.0.0
glob: 7.1.7
jest: 27.0.6
@ -156,7 +154,7 @@ importers:
'@bytemd/plugin-math-ssr': link:../../packages/plugin-math-ssr
'@bytemd/plugin-medium-zoom': link:../../packages/plugin-medium-zoom
'@bytemd/plugin-mermaid': link:../../packages/plugin-mermaid
'@sveltejs/kit': 1.0.0-next.137_svelte@3.38.3
'@sveltejs/kit': 1.0.0-next.139_svelte@3.38.3
bytemd: link:../../packages/bytemd
svelte: 3.38.3
svelte-check: 2.2.3_6ccc96ef45cf08655af73d466cb745f0
@ -3254,8 +3252,8 @@ packages:
'@sinonjs/commons': 1.8.3
dev: true
/@sveltejs/kit/1.0.0-next.137_svelte@3.38.3:
resolution: {integrity: sha512-YdcwaN9zWcbz4jLZS4j9/7fNZIVO8WkXDuwGTiKARhOLIuuJAjCvSuOZ3btJdV/ySpizTWX22u632weVPLECdw==}
/@sveltejs/kit/1.0.0-next.139_svelte@3.38.3:
resolution: {integrity: sha512-V/OG9FIuq8Fzh08x0VZx1ZX5MjCM8lD/vpk/cM/RyGJSky+KKYgch7mWSY6XP28vUsl1HUg+HMuho/rKqmwe9A==}
engines: {node: ^12.20 || >=14.13}
hasBin: true
peerDependencies:
@ -6606,11 +6604,6 @@ packages:
esrecurse: 4.3.0
estraverse: 4.3.0
/esm/3.2.25:
resolution: {integrity: sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==}
engines: {node: '>=6'}
dev: true
/esprima/4.0.1:
resolution: {integrity: sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==}
engines: {node: '>=4'}

View File

@ -1,7 +1,9 @@
// @ts-check
import fs from 'fs-extra'
import path from 'path'
import mustache from 'mustache'
import _ from 'lodash'
import { rootDir } from './utils.mjs'
function readFileSyncSafe(p) {
try {
@ -11,15 +13,15 @@ function readFileSyncSafe(p) {
}
}
const root = path.join(__dirname, '../packages')
const packages = fs.readdirSync(root)
const packagesDir = path.join(rootDir, 'packages')
const packages = fs.readdirSync(packagesDir)
const plugins = packages.filter(
(x) => x.startsWith('plugin-') && !x.includes('-transform')
)
// tsconfig root
fs.writeJsonSync(
path.resolve(__dirname, '../tsconfig.json'),
path.resolve(rootDir, 'tsconfig.json'),
{
files: [],
references: packages.map((p) => {
@ -44,19 +46,19 @@ packages.forEach((p) => {
tsconfig.references = [{ path: '../bytemd' }]
}
fs.writeJsonSync(path.join(root, p, 'tsconfig.json'), tsconfig, {
fs.writeJsonSync(path.join(packagesDir, p, 'tsconfig.json'), tsconfig, {
spaces: 2,
})
// license
fs.copyFileSync(
path.join(__dirname, '../LICENSE'),
path.join(root, p, 'LICENSE')
path.join(rootDir, 'LICENSE'),
path.join(packagesDir, p, 'LICENSE')
)
// package.json
const pkgPath = path.join(root, p, 'package.json')
const pkg = require(pkgPath)
const pkgPath = path.join(packagesDir, p, 'package.json')
const pkg = fs.readJsonSync(pkgPath)
pkg.repository = {
type: 'git',
url: 'https://github.com/bytedance/bytemd.git',
@ -75,23 +77,24 @@ packages.forEach((p) => {
plugins.forEach((p) => {
const name = p.split('-').slice(1).join('-')
const result = mustache.render(
readFileSyncSafe(path.join(__dirname, 'plugin-template.md')),
readFileSyncSafe(path.join(rootDir, 'scripts/plugin-template.md')),
{
name,
importedName: _.camelCase(name.replace('-ssr', '')),
desc: require(path.join(root, p, 'package.json')).description,
desc: fs.readJsonSync(path.join(packagesDir, p, 'package.json'))
.description,
}
)
fs.writeFileSync(path.join(root, p, 'README.md'), result)
fs.writeFileSync(path.join(packagesDir, p, 'README.md'), result)
})
// bytemd readme
const readme = readFileSyncSafe(path.join(__dirname, '../README.md')).replace(
const readme = readFileSyncSafe(path.join(rootDir, 'README.md')).replace(
/## Plugins\s+([\w\W])*?\s+##/,
(match, p1, offset, string) => {
const content = plugins
.map((p) => {
const pkg = require(path.join(root, p, 'package.json'))
const pkg = fs.readJsonSync(path.join(packagesDir, p, 'package.json'))
if (pkg.private) return
const name = p.split('-').slice(1).join('-')
@ -117,13 +120,13 @@ ${content}
}
)
fs.writeFileSync(path.join(__dirname, '../README.md'), readme)
fs.writeFileSync(path.join(rootDir, 'README.md'), readme)
// locales
let importCode = ''
let exportObject = {}
packages.forEach((p) => {
const localeDir = path.join(root, p, 'src/locales')
const localeDir = path.join(packagesDir, p, 'src/locales')
if (fs.existsSync(localeDir) && fs.lstatSync(localeDir).isDirectory()) {
const locales = fs.readdirSync(localeDir).map((x) => x.replace('.json', ''))
const libName = p.startsWith('plugin') ? `@bytemd/${p}` : p

View File

@ -5,6 +5,7 @@ import glob from 'glob'
import chokidar from 'chokidar'
import { preprocess } from 'svelte/compiler'
import autoprocessor from 'svelte-preprocess'
import { rootDir } from './utils.mjs'
// svelte file: compile
// other files: copy
@ -20,7 +21,7 @@ async function transform(file, dir = 'lib') {
source,
autoprocessor({
typescript: {
tsconfigFile: path.resolve(__dirname, '../tsconfig-base.json'),
tsconfigFile: path.resolve(rootDir, 'tsconfig-base.json'),
},
// https://github.com/sveltejs/svelte/issues/189#issuecomment-586142198
replace: [
@ -61,12 +62,12 @@ function build(pattern, dir) {
}
const pattern = path.resolve(
__dirname,
`../packages/*/src/**/*.{svelte,vue,json,wxml,wxss}`
rootDir,
`packages/*/src/**/*.{svelte,vue,json,wxml,wxss}`
)
const mpPattern = path.resolve(
__dirname,
`../packages/mp/src/**/*.{svelte,vue,json,wxml,wxss}`
rootDir,
`packages/mp/src/**/*.{svelte,vue,json,wxml,wxss}`
)
if (process.argv.includes('--watch')) {

6
scripts/utils.mjs Normal file
View File

@ -0,0 +1,6 @@
// @ts-check
import path from 'path'
import { fileURLToPath } from 'url'
// https://stackoverflow.com/a/50052194
export const rootDir = path.resolve(fileURLToPath(import.meta.url), '../..')