Skip to content

Commit

Permalink
chore: 🤖 add build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
lenconda committed Sep 2, 2021
1 parent 2828ce9 commit 0da2d7a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 30 deletions.
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
72 changes: 72 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -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 <packages...>', 'select packages to build')
.action(({ watch = false, packages = [] }) => {
build(watch, packages);
});

program.parse(process.argv);
35 changes: 20 additions & 15 deletions scripts/clean.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};

Expand Down
11 changes: 1 addition & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@
},
"references": [
{
"path": "packages/dollie-cli"
"path": "packages/*"
},
{
"path": "packages/dollie-core"
},
{
"path": "packages/dollie-origins"
},
{
"path": "packages/dollie-utils"
}
]
}
15 changes: 15 additions & 0 deletions utils/paths.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 0da2d7a

Please sign in to comment.