From 0da2d7aa84c8ab93dc4ca1b1a8319b6c4bdb91be Mon Sep 17 00:00:00 2001 From: lenconda Date: Thu, 2 Sep 2021 21:17:32 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20add=20build=20scripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 7 ++--- scripts/build.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ scripts/clean.js | 35 +++++++++++++---------- tsconfig.json | 11 +------- utils/paths.js | 15 ++++++++++ 5 files changed, 110 insertions(+), 30 deletions(-) create mode 100644 scripts/build.js create mode 100644 utils/paths.js diff --git a/package.json b/package.json index f142739..4ace0f8 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,7 @@ "watch:origins": "cd packages/dollie-origins && npm run build:watch", "init-watch": "npm run build", "watch": "concurrently \"npm:watch:*\"", - "build:utils": "cd packages/dollie-utils && npm run build", - "build:cli": "cd packages/dollie-cli && npm run build", - "build:core": "cd packages/dollie-core && npm run build", - "build:origins": "cd packages/dollie-origins && npm run build", - "build": "npm-run-all -s build:utils build:origins build:core build:cli", + "build": "node scripts/build.js", "docs:start": "dumi dev", "docs:build": "dumi build", "docs:deploy": "node scripts/docs.js", @@ -29,6 +25,7 @@ "aliyun-oss-deploy": "^0.1.5", "antd": "^4.16.6", "babel-eslint": "^10.1.0", + "commander": "^8.1.0", "concurrently": "^6.2.0", "dumi": "^1.1.22", "eslint": "^7.28.0", diff --git a/scripts/build.js b/scripts/build.js new file mode 100644 index 0000000..1852b87 --- /dev/null +++ b/scripts/build.js @@ -0,0 +1,72 @@ +const getPaths = require('../utils/paths'); +const path = require('path'); +const { program } = require('commander'); +const { execSync } = require('child_process'); +const fs = require('fs-extra'); + +const build = (watch = false, selectedPackages = []) => { + const paths = getPaths(); + const packages = paths.map((currentPath) => { + const { + name, + dependencies = {}, + devDependencies = {}, + } = require(path.resolve(currentPath, 'package.json')); + + return { + pathname: currentPath, + name, + dependencies: Object.keys(dependencies).concat(Object.keys(devDependencies)), + }; + }); + + const projectPackages = packages.map((package) => { + const { + name, + pathname, + dependencies, + } = package; + + return { + name, + pathname, + dependencies: dependencies.filter((dependencyName) => { + return packages.findIndex((package) => dependencyName === package.name) !== -1; + }), + }; + }).sort((previousPackage, nextPackage) => { + if (nextPackage.dependencies.findIndex((dependencyName) => dependencyName === previousPackage.name) !== -1) { + return -1; + } else if (previousPackage.dependencies.findIndex((dependencyName) => dependencyName === nextPackage.name) !== -1) { + return 1; + } else { + return 0; + } + }); + + for (const projectPackage of projectPackages) { + const { + name, + pathname, + } = projectPackage; + + if (selectedPackages.length > 0 && selectedPackages.indexOf(name) === -1) { + continue; + } + + console.log(`[BUILD]${watch ? '[WATCH]' : ''}`, name); + fs.removeSync(path.resolve(pathname, './lib')); + execSync(`tsc${watch ? '--watch' : ''}`, { + cwd: pathname, + }); + } +}; + +program + .option('-w, --watch', 'watch file changes') + .option('-p, --packages ', 'select packages to build') + .action(({ watch = false, packages = [] }) => { + build(watch, packages); + }); + +program.parse(process.argv); diff --git a/scripts/clean.js b/scripts/clean.js index f87fa5a..2d385a3 100644 --- a/scripts/clean.js +++ b/scripts/clean.js @@ -1,25 +1,30 @@ -const glob = require('glob'); const path = require('path'); -const { packages = [] } = require(path.resolve(__dirname, '../lerna.json')); const fs = require('fs-extra'); +const getPaths = require('../utils/paths'); const clean = () => { + const paths = getPaths(); const cwd = path.resolve(__dirname, '..'); - const paths = packages.reduce((result, pattern) => { - const paths = glob.sync(pattern, { - cwd, - }); - return result.concat(paths); - }, []); + + const fileMap = { + lib: './lib', + modules: './node_modules', + lock: './package-lock.json', + }; + + const types = process.argv[2] + ? process.argv[2].split(',') + : Object.keys(fileMap); for (const packagePath of paths) { - const libPath = path.resolve(cwd, packagePath, './lib'); - const nodeModulesPath = path.resolve(cwd, packagePath, './node_modules'); - const lockFilePath = path.resolve(cwd, packagePath, './package-lock.json'); - console.log('[CLEAN] ', packagePath); - fs.removeSync(libPath); - fs.removeSync(nodeModulesPath); - fs.removeSync(lockFilePath); + for (const type of types) { + if (!fileMap[type]) { + continue; + } + const currentPath = path.resolve(cwd, packagePath, fileMap[type]); + console.log('[CLEAN]', currentPath); + fs.removeSync(currentPath); + } } }; diff --git a/tsconfig.json b/tsconfig.json index 7646748..88fa725 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,16 +5,7 @@ }, "references": [ { - "path": "packages/dollie-cli" + "path": "packages/*" }, - { - "path": "packages/dollie-core" - }, - { - "path": "packages/dollie-origins" - }, - { - "path": "packages/dollie-utils" - } ] } diff --git a/utils/paths.js b/utils/paths.js new file mode 100644 index 0000000..bd8fd36 --- /dev/null +++ b/utils/paths.js @@ -0,0 +1,15 @@ +const path = require('path'); +const glob = require('glob'); + +module.exports = () => { + const { packages = [] } = require(path.resolve(__dirname, '../lerna.json')); + + const paths = packages.reduce((result, pattern) => { + const paths = glob.sync(pattern, { + cwd: path.resolve(__dirname, '..'), + }); + return result.concat(paths); + }, []); + + return paths; +};