Skip to content

Commit 3787df9

Browse files
authored
👌 IMPROVE: Remove mz modules deps (#405)
mz, mz-modules, uuid
1 parent dbb3b7a commit 3787df9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+236
-248
lines changed

‎bin/install.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
'use strict';
44

55
const debug = require('debug')('npminstall:bin:install');
6-
const npa = require('../lib/npa');
76
const chalk = require('chalk');
87
const path = require('path');
98
const util = require('util');
10-
const execSync = require('child_process').execSync;
11-
const fs = require('mz/fs');
9+
const { execSync } = require('child_process');
10+
const fs = require('fs/promises');
11+
const { writeFileSync } = require('fs');
1212
const parseArgs = require('minimist');
13+
const { installLocal, installGlobal } = require('..');
14+
const npa = require('../lib/npa');
1315
const utils = require('../lib/utils');
1416
const globalConfig = require('../lib/config');
15-
const installLocal = require('..').installLocal;
16-
const installGlobal = require('..').installGlobal;
1717
const { parsePackageName } = require('../lib/alias');
1818
const {
1919
LOCAL_TYPES,
@@ -306,7 +306,7 @@ debug('argv: %j, env: %j', argv, env);
306306
const dependenciesTree = argv['dependencies-tree'];
307307
if (dependenciesTree) {
308308
try {
309-
const content = fs.readFileSync(dependenciesTree);
309+
const content = await fs.readFile(dependenciesTree);
310310
config.dependenciesTree = JSON.parse(content);
311311
} catch (err) {
312312
console.warn(chalk.yellow('npminstall WARN load dependencies tree %s error: %s'), dependenciesTree, err.message);
@@ -332,7 +332,7 @@ debug('argv: %j, env: %j', argv, env);
332332
if (config.production) {
333333
// warning when `${root}/node_modules` exists
334334
const nodeModulesDir = path.join(root, 'node_modules');
335-
if (await fs.exists(nodeModulesDir)) {
335+
if (await utils.exists(nodeModulesDir)) {
336336
const dirs = await fs.readdir(nodeModulesDir);
337337
// ignore [ '.bin', 'node' ], it will install first by https://github.com/cnpm/nodeinstall
338338
if (!(dirs.length === 2 && dirs.indexOf('.bin') >= 0 && dirs.indexOf('node') >= 0)) {
@@ -341,7 +341,7 @@ debug('argv: %j, env: %j', argv, env);
341341
}
342342
}
343343
const pkgFile = path.join(root, 'package.json');
344-
const exists = await fs.exists(pkgFile);
344+
const exists = await utils.exists(pkgFile);
345345
if (!exists) {
346346
console.warn(chalk.yellow(`npminstall WARN package.json not exists: ${pkgFile}`));
347347
} else {
@@ -420,7 +420,7 @@ debug('argv: %j, env: %j', argv, env);
420420

421421
process.on('exit', code => {
422422
if (code !== 0) {
423-
fs.writeFileSync(path.join(root, 'npminstall-debug.log'), util.inspect(config, { depth: 2 }));
423+
writeFileSync(path.join(root, 'npminstall-debug.log'), util.inspect(config, { depth: 2 }));
424424
}
425425
});
426426
})().catch(err => {

‎bin/link.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const semver = require('semver');
88
const assert = require('assert');
99
const chalk = require('chalk');
1010
const path = require('path');
11-
const fs = require('mz/fs');
1211
const parseArgs = require('minimist');
1312
const utils = require('../lib/utils');
1413
const bin = require('../lib/bin');
@@ -139,7 +138,7 @@ const folders = argv._.map(name => utils.formatPath(name));
139138
folder = path.join(root, folder);
140139
}
141140
// read from folder
142-
if (!(await fs.exists(folder))) {
141+
if (!(await utils.exists(folder))) {
143142
throw new Error(`${folder} not exists`);
144143
}
145144

‎bin/uninstall.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const debug = require('debug')('npminstall:bin:uninstall');
66
const npa = require('npm-package-arg');
77
const path = require('path');
8-
const fs = require('mz/fs');
8+
const fs = require('fs/promises');
99
const parseArgs = require('minimist');
1010
const chalk = require('chalk');
1111
const execSync = require('child_process').execSync;

‎bin/update.js‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,10 @@
33
'use strict';
44

55
const path = require('path');
6-
const rimraf = require('mz-modules/rimraf');
76
const parseArgs = require('minimist');
7+
const { rimraf } = require('../lib/utils');
88

9-
const argv = parseArgs(process.argv.slice(2), {
10-
string: [
11-
'root',
12-
],
13-
boolean: [
14-
'help',
15-
],
16-
alias: {
17-
h: 'help',
18-
},
19-
});
20-
21-
if (argv.help) return help();
22-
23-
const root = argv.root || process.cwd();
24-
25-
function help() {
9+
function help(root) {
2610
console.log(`
2711
Usage:
2812
@@ -33,6 +17,20 @@ Usage:
3317
}
3418

3519
(async () => {
20+
const argv = parseArgs(process.argv.slice(2), {
21+
string: [
22+
'root',
23+
],
24+
boolean: [
25+
'help',
26+
],
27+
alias: {
28+
h: 'help',
29+
},
30+
});
31+
32+
const root = argv.root || process.cwd();
33+
if (argv.help) return help(root);
3634
const nodeModules = path.join(root, 'node_modules');
3735
console.log('[npmupdate] removing %s', nodeModules);
3836
await rimraf(nodeModules);

‎lib/download/git.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4-
const uuid = require('uuid');
4+
const { randomUUID } = require('crypto');
55
const chalk = require('chalk');
66
const pacote = require('pacote');
77
const utils = require('../utils');
@@ -15,7 +15,7 @@ module.exports = async (pkg, options) => {
1515

1616
options.gitPackages++;
1717
options.console.warn(chalk.yellow(`[${displayName}] install ${name || ''} from git ${raw}, may be very slow, please keep patience`));
18-
const cloneDir = path.join(options.storeDir, '.tmp', uuid());
18+
const cloneDir = path.join(options.storeDir, '.tmp', randomUUID());
1919
await utils.mkdirp(cloneDir);
2020
try {
2121
const resolveResult = await pacote.extract(raw, cloneDir);

‎lib/download/local.js‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22

33
const debug = require('debug')('npminstall:download:local');
4-
const cp = require('mz/child_process');
5-
const rimraf = require('mz-modules/rimraf');
6-
const fs = require('mz/fs');
4+
const { randomUUID } = require('crypto');
5+
const fs = require('fs/promises');
6+
const { createReadStream } = require('fs');
77
const path = require('path');
88
const chalk = require('chalk');
9-
const uuid = require('uuid');
109
const utils = require('../utils');
1110

1211
module.exports = async (pkg, options) => {
@@ -32,27 +31,27 @@ async function localFolder(filepath, pkg, options) {
3231
debug(`install ${pkg.name}@${pkg.rawSpec} from local folder ${filepath}`);
3332
try {
3433
// use npm pack to ensure npmignore/gitignore/package.files work fine
35-
const res = await cp.exec('npm pack', { cwd: filepath });
36-
if (res && res[0]) {
37-
const tarball = path.join(filepath, res[0].trim());
34+
const res = await utils.exec('npm pack', { cwd: filepath });
35+
if (res && res.stdout) {
36+
const tarball = path.join(filepath, res.stdout.trim());
3837
try {
3938
return await localTarball(tarball, pkg, options);
4039
} finally {
41-
await rimraf(tarball);
40+
await utils.rimraf(tarball);
4241
}
4342
}
4443
} catch (err) {
4544
// fallback to copy
46-
debug(`install ${pkg.name}@${pkg.rawSpec} from local folder ${filepath} with npm pack failed(${err.message}), use copy`);
45+
options.console.warn(`[npminstall:download:local] install ${pkg.displayName} from local folder ${filepath} with npm pack failed(${err.message}), use copy`);
4746
return await utils.copyInstall(filepath, options);
4847
}
4948
}
5049

5150
async function localTarball(filepath, pkg, options) {
5251
debug(`install ${pkg.name}@${pkg.rawSpec} from local tarball ${filepath}`);
53-
const readstream = fs.createReadStream(filepath);
52+
const readstream = createReadStream(filepath);
5453
// everytime unpack to a different directory
55-
const ungzipDir = path.join(options.storeDir, '.tmp', uuid());
54+
const ungzipDir = path.join(options.storeDir, '.tmp', randomUUID());
5655
await utils.mkdirp(ungzipDir);
5756
try {
5857
await utils.unpack(readstream, ungzipDir, pkg);
@@ -62,7 +61,7 @@ async function localTarball(filepath, pkg, options) {
6261
try {
6362
await utils.rimraf(ungzipDir);
6463
} catch (err) {
65-
options.console.warn(chalk.yellow(`rmdir local ungzip dir: ${ungzipDir} error: ${err}, ignore it`));
64+
options.console.warn(chalk.yellow(`[npminstall:download:local] ${pkg.displayName} rmdir local ungzip dir: ${ungzipDir} error: ${err}, ignore it`));
6665
}
6766
}
6867
}

‎lib/download/npm.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const debug = require('debug')('npminstall:download:npm');
44
const bytes = require('bytes');
5+
const { randomUUID } = require('crypto');
56
const { createWriteStream, createReadStream, rmSync } = require('fs');
67
const fs = require('fs/promises');
78
const path = require('path');
@@ -11,7 +12,6 @@ const zlib = require('zlib');
1112
const destroy = require('destroy');
1213
const urlresolve = require('url').resolve;
1314
const chalk = require('chalk');
14-
const uuid = require('uuid');
1515
const moment = require('moment');
1616
const os = require('os');
1717
const semver = require('semver');
@@ -573,7 +573,7 @@ async function getTarballStream(tarballUrl, pkg, options) {
573573
const tmpDir = path.join(options.cacheDir, '.tmp', moment().format('YYYY/MM/DD'));
574574
await utils.mkdirp(parentDir);
575575
await utils.mkdirp(tmpDir);
576-
const tmpFile = path.join(tmpDir, `${name}-${pkg.version}-${uuid.v4()}.tgz`);
576+
const tmpFile = path.join(tmpDir, `${name}-${pkg.version}-${randomUUID()}.tgz`);
577577
const result = await get(tarballUrl, {
578578
timeout: options.streamingTimeout || options.timeout,
579579
followRedirect: true,

‎lib/download/remote.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4-
const uuid = require('uuid');
4+
const { randomUUID } = require('crypto');
55
const chalk = require('chalk');
66
const utils = require('../utils');
77

@@ -17,7 +17,7 @@ module.exports = async (pkg, options) => {
1717
const remoteUrl = fetchSpec;
1818
options.console.warn(chalk.yellow(`[${displayName}] install ${name || ''} from remote ${remoteUrl}, may be very slow, please keep patience`));
1919
const readstream = await utils.getTarballStream(remoteUrl, options);
20-
const ungzipDir = path.join(options.storeDir, '.tmp', uuid());
20+
const ungzipDir = path.join(options.storeDir, '.tmp', randomUUID());
2121
await utils.mkdirp(ungzipDir);
2222
try {
2323
await utils.unpack(readstream, ungzipDir, pkg);

‎lib/format_install_options.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const debug = require('debug')('npminstall:format_install_options');
44
const os = require('os');
5-
const uuid = require('uuid');
5+
const { randomUUID } = require('crypto');
66
const awaitEvent = require('await-event');
77
const assert = require('assert');
88
const path = require('path');
@@ -124,7 +124,7 @@ module.exports = function formatInstallOptions(options) {
124124
if (options.production) {
125125
options.referer += ' --production';
126126
}
127-
options.referer += ' --uuid=' + uuid.v1();
127+
options.referer += ` --uuid=${randomUUID()}`;
128128

129129
options.installRoot = options.pkgs.length === 0;
130130

‎lib/global_install.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
const path = require('path');
88
const utility = require('utility');
9-
const fs = require('mz/fs');
9+
const fs = require('fs/promises');
1010
const chalk = require('chalk');
1111
const npa = require('./npa');
1212
const installLocal = require('./local_install');

0 commit comments

Comments
 (0)