Compare commits
6 Commits
bce054516b
...
9aebeb31a6
| Author | SHA1 | Date | |
|---|---|---|---|
| 9aebeb31a6 | |||
| b93defbf4d | |||
| 4cd6e2922c | |||
| 9fb86bf32e | |||
| 71f8bb7729 | |||
| 4fec26c796 |
@@ -9,7 +9,8 @@
|
||||
"scripts": {
|
||||
"lint": "eslint 'src/**/*.{js,mjs}'",
|
||||
"version": "npm-auto-version",
|
||||
"start": "node ."
|
||||
"start": "node .",
|
||||
"test": "npm run lint"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -63,7 +64,7 @@
|
||||
"trailingComma": "es5"
|
||||
},
|
||||
"dependencies": {
|
||||
"fast-deep-equal": "^2.0.1",
|
||||
"glob-parent": "^5.0.0",
|
||||
"joycon": "^2.2.4",
|
||||
"md5-file": "^4.0.0",
|
||||
"mkdirp": "^0.5.1",
|
||||
|
||||
@@ -36,7 +36,9 @@ function buildConfig(args) {
|
||||
// parse args from CLI
|
||||
const args = parseArgs();
|
||||
|
||||
pkgcomp(buildConfig(args), utils.getIdent(), args).catch(err => {
|
||||
const { name } = utils.getPackageInfo();
|
||||
|
||||
pkgcomp(buildConfig(args), name, args).catch(err => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(err);
|
||||
process.exit(err.exitCode);
|
||||
|
||||
@@ -18,7 +18,7 @@ exports.read = (fileRoot, ident) => {
|
||||
|
||||
try {
|
||||
const data = JSON.parse(fs.readFileSync(filePath));
|
||||
return data[ident] || {};
|
||||
return JSON.stringify(data[ident] || {});
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
fs.writeFileSync(filePath, JSON.stringify({}));
|
||||
|
||||
17
src/index.js
17
src/index.js
@@ -1,6 +1,5 @@
|
||||
const path = require('path');
|
||||
const md5File = require('md5-file');
|
||||
const deepEqual = require('fast-deep-equal');
|
||||
const shellExec = require('shell-exec');
|
||||
const data = require('./data');
|
||||
const utils = require('./utils');
|
||||
@@ -43,14 +42,25 @@ module.exports = async function pkgcomp(config, ident, opts = defaultOptions) {
|
||||
// read dependencies from package.json
|
||||
const pkg = utils.getFileContents(path.join(config.rootDir, 'package.json'), { format: 'json' });
|
||||
|
||||
// read dependencies from workspace packages
|
||||
const workspaces = utils.getPackageWorkspaces().map(({ name, package }) => {
|
||||
const wpkg = utils.getFileContents(path.join(config.rootDir, package), { format: 'json' });
|
||||
return {
|
||||
name,
|
||||
dependencies: wpkg.dependencies,
|
||||
devDependencies: wpkg.devDependencies,
|
||||
};
|
||||
});
|
||||
|
||||
const payload = {
|
||||
hashes,
|
||||
dependencies: pkg.dependencies,
|
||||
devDependencies: pkg.devDependencies,
|
||||
workspaces,
|
||||
};
|
||||
|
||||
// check if the command should be run
|
||||
const runCmd = Boolean(opts.force || (doExec && !deepEqual(packageHistory, payload)));
|
||||
const runCmd = Boolean(opts.force || (doExec && packageHistory !== JSON.stringify(payload)));
|
||||
|
||||
if (runCmd) {
|
||||
const res = await execCommand(config.cmd);
|
||||
@@ -60,6 +70,9 @@ module.exports = async function pkgcomp(config, ident, opts = defaultOptions) {
|
||||
err.cmd = res.cmd;
|
||||
throw err;
|
||||
}
|
||||
} else if (opts.verbose) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('No changes, command skipped');
|
||||
}
|
||||
|
||||
data.write(config.dataDir, ident, payload);
|
||||
|
||||
34
src/utils.js
34
src/utils.js
@@ -2,6 +2,7 @@ const path = require('path');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const JoyCon = require('joycon');
|
||||
const globParent = require('glob-parent');
|
||||
|
||||
exports.getRootPath = () => process.cwd();
|
||||
|
||||
@@ -27,13 +28,36 @@ exports.getFileContents = (filePath, opts = {}) => {
|
||||
return content;
|
||||
};
|
||||
|
||||
exports.getRootFileContents = (filename, opts = {}) => {
|
||||
return exports.getFileContents(path.join(exports.getRootPath(), filename), opts);
|
||||
exports.getPackageInfo = () => {
|
||||
const { name, workspaces } = exports.getFileContents(
|
||||
path.join(exports.getRootPath(), 'package.json'),
|
||||
{
|
||||
format: 'json',
|
||||
}
|
||||
);
|
||||
if (!name) throw new Error('Unable to read project name from package.json');
|
||||
return { name, workspaces };
|
||||
};
|
||||
|
||||
exports.getIdent = () => {
|
||||
const { name } = exports.getRootFileContents('package.json', { format: 'json' });
|
||||
return name;
|
||||
exports.getPackageWorkspaces = () => {
|
||||
const { workspaces } = exports.getPackageInfo();
|
||||
if (!workspaces) return [];
|
||||
|
||||
return workspaces.reduce((acc, glob) => {
|
||||
const parent = globParent(glob);
|
||||
const packages = fs
|
||||
.readdirSync(parent)
|
||||
.map(package => {
|
||||
const packageJson = path.join(parent, package, 'package.json');
|
||||
if (!exports.canAccessFile(packageJson)) return false;
|
||||
return {
|
||||
name: package,
|
||||
package: packageJson,
|
||||
};
|
||||
})
|
||||
.filter(Boolean);
|
||||
return acc.concat(packages);
|
||||
}, []);
|
||||
};
|
||||
|
||||
exports.getConfig = (overrides = {}) => {
|
||||
|
||||
@@ -1027,6 +1027,13 @@ getopts@^2.0.6:
|
||||
resolved "https://registry.yarnpkg.com/getopts/-/getopts-2.2.4.tgz#3137fe8a5fddf304904059a851bdc1c22f0f54fb"
|
||||
integrity sha512-Rz7DGyomZjrenu9Jx4qmzdlvJgvrEFHXHvjK0FcZtcTC1U5FmES7OdZHUwMuSnEE6QvBvwse1JODKj7TgbSEjQ==
|
||||
|
||||
glob-parent@^5.0.0:
|
||||
version "5.0.0"
|
||||
resolved "https://npm.w33ble.com:443/glob-parent/-/glob-parent-5.0.0.tgz#1dc99f0f39b006d3e92c2c284068382f0c20e954"
|
||||
integrity sha512-Z2RwiujPRGluePM6j699ktJYxmPpJKCfpGA13jz2hmFZC7gKetzrWvg5KN3+OsIFmydGyZ1AVwERCq1w/ZZwRg==
|
||||
dependencies:
|
||||
is-glob "^4.0.1"
|
||||
|
||||
glob@^7.0.3, glob@^7.1.2, glob@^7.1.3:
|
||||
version "7.1.3"
|
||||
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1"
|
||||
@@ -1324,7 +1331,7 @@ is-fullwidth-code-point@^2.0.0:
|
||||
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f"
|
||||
integrity sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=
|
||||
|
||||
is-glob@^4.0.0:
|
||||
is-glob@^4.0.0, is-glob@^4.0.1:
|
||||
version "4.0.1"
|
||||
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.1.tgz#7567dbe9f2f5e2467bc77ab83c4a29482407a5dc"
|
||||
integrity sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==
|
||||
|
||||
Reference in New Issue
Block a user