node-power-user 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -38,19 +38,72 @@ npm i -g node-power-user
38
38
  ## 📘 Example Setup
39
39
  After installing via NPM, you can use the CLI with the `npu` command.
40
40
 
41
- ## 💻 Example CLI Usage
41
+ <!-- ## 💻 Example CLI Usage
42
42
  Note: you may have to run cli commands with `npx npu <command>` if you install this package locally.
43
- * `npu v`: Check version of node-power-user.
44
- * `npu pv`: Check version of the current project.
43
+ * `npu -v`: Check version of node-power-user.
45
44
  * `npu clean`: Clean your node project (runs `rm -fr node_modules && rm -fr package-lock.json && npm cache clean --force && npm install && npm rb`).
46
45
  * `npu bump`: Bump your project's version.
47
- * `npu bump 1`: Bump the last number (`patch` version).
48
- * `npu bump 2`: Bump the middle number (`minor` version).
49
- * `npu bump 3`: Bump the first number (`major` version).
46
+ * `npu bump patch`: Bump the last number
47
+ * `npu bump minor`: Bump the middle number
48
+ * `npu bump major`: Bump the first number
50
49
  * `npu outdated`: Compare the versions of installed modules to those in your package.json
51
50
  * `npu global`: List all global packages for all versions of Node.js on your machine (must use NVM).
52
- ### Flags
53
- * `--wait`: Wait the specified amount of time in milliseconds.
51
+ * `npu sync`: Pull the latest changes from the remote repository and push your changes.
52
+ * `npu packages`: List all packages in your project. -->
53
+ ## 💻 Example CLI Usage
54
+
55
+ ### Bump Version
56
+ Bump your project's version by the specified level.
57
+ ```shell
58
+ npu bump <level>
59
+ ```
60
+ * `npu bump patch`: Bump the last number
61
+ * `npu bump minor`: Bump the middle number
62
+ * `npu bump major`: Bump the first number
63
+
64
+ ### Clean Project
65
+ Clean your node project (runs `rm -fr node_modules && rm -fr package-lock.json && npm cache clean --force && npm install && npm rb`).
66
+ ```shell
67
+ npu clean
68
+ ```
69
+
70
+ ### Global Packages
71
+ List all global packages for all versions of Node.js on your machine (you **must have NVM** installed).
72
+ ```shell
73
+ npu global
74
+ ```
75
+
76
+ ### Outdated Packages
77
+ Compare the versions of installed modules to those in your package.json
78
+ ```shell
79
+ npu outdated
80
+ ```
81
+
82
+ ### List Packages
83
+ List all packages in your project.
84
+ ```shell
85
+ npu packages
86
+ ```
87
+
88
+ ### Sync Changes
89
+ Pull the latest changes from the remote repository and push your changes. You can optionally supply a `--message="Your commit message here"` flag.
90
+ ```shell
91
+ npu sync
92
+ ```
93
+
94
+ ### Check Version
95
+ Check the version of node-power-user.
96
+ ```shell
97
+ npu -v
98
+ ```
99
+
100
+ ### Wait
101
+ Wait for a specified number of ms.
102
+ ```shell
103
+ npu wait <ms>
104
+ ```
105
+
106
+ ### Global flags
54
107
  * `--debug`: Log the commands and flags before they are executed
55
108
 
56
109
  ## 🗨️ Final Words
package/dist/cli.js CHANGED
@@ -11,7 +11,7 @@ const ALIASES = {
11
11
  outdated: ['-o', 'out', '--outdated'],
12
12
  packages: ['-p', 'pack', '--packages'],
13
13
  version: ['-v', '--version'],
14
- sync: ['-s', '--sync'],
14
+ sync: ['-s', '--sync', 'push', '--push'],
15
15
  };
16
16
 
17
17
  // Function to resolve command name from aliases
@@ -43,6 +43,10 @@ function resolveCommand(options) {
43
43
  function Main() {}
44
44
 
45
45
  Main.prototype.process = async function (options) {
46
+ // Fix options
47
+ options = options || {};
48
+ options._ = options._ || [];
49
+
46
50
  // Determine the command (use default if not provided)
47
51
  const command = resolveCommand(options);
48
52
 
@@ -0,0 +1,33 @@
1
+ // Libraries
2
+ const logger = new (require('../lib/logger'))('node-power-user');
3
+ const path = require('path');
4
+ const jetpack = require('fs-jetpack');
5
+ const { wait } = require('node-powertools');
6
+
7
+ // Load package
8
+ const package = jetpack.read(path.join(__dirname, '../../', 'package.json'), 'json');
9
+ const project = jetpack.read(path.join(process.cwd(), 'package.json'), 'json');
10
+
11
+ module.exports = async function (options) {
12
+ // Fix options
13
+ options = options || {};
14
+ options.time = options.time || parseInt(options._[1]) || 0;
15
+
16
+ // Log initial state
17
+ logger.log(`Waiting for ${options.time}ms...`);
18
+
19
+ try {
20
+ // Run cleanup commands
21
+ await wait(options.time);
22
+
23
+ // Log success
24
+ logger.log(logger.format.green('Wait completed successfully!'));
25
+
26
+ return true;
27
+ } catch (e) {
28
+ // Log failure
29
+ logger.error(`Wait failed`, e.stack);
30
+
31
+ return false;
32
+ }
33
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-power-user",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Easy tools for every Node.js developer!",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/dist/index.js DELETED
@@ -1,432 +0,0 @@
1
- // CLI GUIDE:
2
- // https://www.twilio.com/blog/how-to-build-a-cli-with-node-js
3
- // https://www.npmjs.com/package/@dkundel/create-project
4
-
5
- // https://www.sitepoint.com/javascript-command-line-interface-cli-node-js/
6
- // https://github.com/sitepoint-editors/ginit
7
- const jetpack = require('fs-jetpack');
8
- const chalk = require('chalk');
9
- const _ = require('lodash');
10
- const inquirer = require('inquirer');
11
- const path = require('path');
12
- const { spawn, exec } = require('child_process');
13
- const argv = require('yargs').argv;
14
- const JSON5 = require('json5');
15
- const powertools = require('node-powertools');
16
- const { isEqual } = require('lodash');
17
- const semverIsEqual = require('semver/functions/eq')
18
- const semverCoerce = require('semver/functions/coerce')
19
- const table = require('table').table;
20
- const ProgressBar = require('cli-progress');
21
- const Npm = require('npm-api');
22
- const os = require('os');
23
-
24
- // const npm = require('npm');
25
-
26
- function Main() {
27
- const self = this;
28
-
29
- self.options = {};
30
- self.npu_packageJSON = {};
31
- self.proj_path = null;
32
- self.proj_packageJSONPath = null;
33
- self.proj_packageJSON = {};
34
- self.proj_allDependencies = {};
35
-
36
- }
37
-
38
- Main.prototype.process = async function (options) {
39
- const self = this;
40
- self.options = options || {};
41
-
42
- // console.log('--process.env', process.env);
43
-
44
- try {
45
- self.npu_packageJSON = require('../package.json');
46
- } catch (e) {
47
- throw new Error(`NPU does not contain a valid package.json file!: \n${e}`);
48
- }
49
-
50
- try {
51
- self.proj_path = process.cwd();
52
- self.proj_packageJSONPath = path.resolve(self.proj_path, './package.json');
53
- self.proj_packageJSON = require(self.proj_packageJSONPath);
54
- self.proj_allDependencies.pairs = _.merge({}, self.proj_packageJSON.peerDependencies, self.proj_packageJSON.devDependencies, self.proj_packageJSON.dependencies);
55
- self.proj_allDependencies.list = Object.keys(self.proj_allDependencies.pairs);
56
-
57
- } catch (e) {
58
- self.proj_packageJSON = {
59
- name: 'Unknown Name',
60
- version: '0.0.0',
61
- dependencies: {},
62
- devDependencies: {},
63
- peerDependencies: {},
64
- allDependencies: {},
65
- }
66
- console.error(chalk.red(`This project does not contain a valid package.json file!: \n${e}`));
67
- }
68
-
69
- // console.log('---args', args);
70
- // if (Array.isArray(args)) {
71
- // for (var i = 0; i < args.length; i++) {
72
- // self.options[args[i]] = true;
73
- // }
74
- // } else {
75
- // Object.keys(args)
76
- // .forEach((arg, i) => {
77
- // self.options[arg] = args[arg];
78
- // });
79
- // }
80
- Object.keys(argv)
81
- .forEach(arg => {
82
- self.options[arg] = argv[arg]
83
- });
84
-
85
- if (self.options.d || self.options.debug || self.options._.includes('debug') || process.env.NPU_LOG === 'true') {
86
- console.log('argv', argv);
87
- console.log('options', self.options);
88
- }
89
-
90
- if (self.options.wait || self.options._.includes('wait')) {
91
- const time = parseInt(typeof self.options.wait === 'number' ? self.options.wait : 1000);
92
- self.log(chalk.blue(`Waiting ${time}...`));
93
- await powertools.wait(time);
94
- self.log(chalk.green(`Done waiting`));
95
- }
96
-
97
- if (self.options.v || self.options.version || self.options._.includes('version')) {
98
- self.log(chalk.blue(`Node Power User is v${chalk.bold(self.npu_packageJSON.version)}`));
99
- return self.npu_packageJSON.version;
100
- }
101
-
102
- if (self.options.pv || self.options.project || self.options['project-version'] || self.options._.includes('project')) {
103
- self.log(chalk.blue(`The current project (${chalk.bold(self.proj_packageJSON.name)}) is v${chalk.bold(self.proj_packageJSON.version)}`));
104
- return self.proj_packageJSON.version;
105
- }
106
-
107
- if (self.options.lp || self.options.listpackages || self.options['list-packages'] || self.options._.includes('listpackages')) {
108
- self.log(chalk.blue.bold(`Dependencies:`));
109
-
110
- Object.keys(self.proj_packageJSON.dependencies || {})
111
- .forEach((dep, i) => {
112
- self.log(chalk.blue(`${dep} @ ${self.proj_packageJSON.dependencies[dep]}`));
113
- });
114
-
115
- self.log(chalk.blue.bold(`\nDev Dependencies:`));
116
- Object.keys(self.proj_packageJSON.devDependencies || {})
117
- .forEach((dep, i) => {
118
- self.log(chalk.blue(`${dep} @ ${self.proj_packageJSON.devDependencies[dep]}`));
119
- });
120
-
121
- self.log(chalk.blue.bold(`\nPeer Dependencies:`));
122
- Object.keys(self.proj_packageJSON.peerDependencies || {})
123
- .forEach((dep, i) => {
124
- self.log(chalk.blue(`${dep} @ ${self.proj_packageJSON.peerDependencies[dep]}`));
125
- });
126
-
127
- return {
128
- dependencies: self.proj_packageJSON.dependencies,
129
- devDependencies: self.proj_packageJSON.devDependencies,
130
- peerDependencies: self.proj_packageJSON.peerDependencies,
131
- };
132
- }
133
-
134
- if (self.options.out || self.options.outdated || self.options.match || self.options._.includes('out') || self.options._.includes('outdated') || self.options._.includes('match')) {
135
- // self.log(chalk.blue.bold(`Outdated:`));
136
- // self.log(chalk.green(`name: package = installed`));
137
-
138
- const data = [
139
- ['Package', 'package.json', 'Intalled', 'Latest'],
140
- ];
141
-
142
- const config = {
143
- columnDefault: {
144
- // width: 10,
145
- },
146
- header: {
147
- alignment: 'center',
148
- content: 'Outdated and mismatched packages',
149
- },
150
- }
151
- const progress = new ProgressBar.SingleBar({}, ProgressBar.Presets.shades_classic);
152
- progress.start(self.proj_allDependencies.list.length, 0);
153
-
154
- const response = {};
155
-
156
- let bumpCommand = '';
157
-
158
- for (var i = 0; i < self.proj_allDependencies.list.length; i++) {
159
- progress.update(i);
160
- const dep = self.proj_allDependencies.list[i];
161
- const depPair = self.proj_allDependencies.pairs[dep];
162
- const packageVersion = _coerce(depPair);
163
- const installedVersion = _coerce(
164
- _getListVersion(
165
- (await asyncCommand(`npm list ${dep} --depth=0 | grep ${dep}`)).split(' ')
166
- )
167
- );
168
-
169
- // console.log('---', dep, depPair, _.get(packageVersion, 'version'), _.get(installedVersion, 'version'));
170
-
171
- // console.log('---dep', dep, packageVersion, installedVersion);
172
- const latestVersion = await getLatestVersion(dep);
173
- const isEqual = _isEqual(installedVersion, packageVersion);
174
- const isLatest = _isEqual(packageVersion, latestVersion);
175
- const verbLocal = isEqual ? 'green' : 'yellow';
176
- const verbRemote = isLatest ? 'green' : 'red';
177
-
178
- if (!isEqual) {
179
- bumpCommand += `npm i ${dep}@${installedVersion} && `
180
- }
181
-
182
- // function _color(array) {
183
- // array.forEach((item, i) => {
184
- // array[i] = chalk[verb](item);
185
- // });
186
- // return array;
187
- // }
188
- // data.push(_color([ dep, packageVersion, installedVersion, latestVersion]))
189
-
190
- // function _color(s) {
191
- // return chalk[verb](s);
192
- // }
193
-
194
- data.push([
195
- dep,
196
- packageVersion,
197
- chalk[verbLocal](installedVersion),
198
- chalk[verbRemote](latestVersion),
199
- ])
200
-
201
- response[dep] = {
202
- isEqual: isEqual,
203
- isLatest: isLatest,
204
- package: packageVersion,
205
- installed: installedVersion,
206
- latest: latestVersion,
207
- }
208
-
209
- // self.log(chalk[verb](`${dep}: ${packageVersion} = ${installedVersion}`));
210
- };
211
-
212
- // self.log(chalk.blue.bold(`\nDev Dependencies:`));
213
- // Object.keys(self.proj_packageJSON.devDependencies || {})
214
- // .forEach((dep, i) => {
215
- // self.log(chalk.blue(`${dep} @ ${self.proj_packageJSON.devDependencies[dep]}`));
216
- // });
217
-
218
- progress.stop();
219
-
220
- console.log(table(data, config));
221
-
222
- if (bumpCommand) {
223
- bumpCommand = bumpCommand.replace(/&&\s$/ig, '')
224
- inquirer.prompt([
225
- {
226
- type: 'confirm',
227
- name: 'bump',
228
- message: 'Would you like to bump the package.json versions?',
229
- default: true,
230
- }
231
- ])
232
- .then(async (answer) => {
233
- if (answer.bump) {
234
- asyncCommand(bumpCommand);
235
- } else {
236
- }
237
- })
238
- }
239
-
240
- return response;
241
- }
242
-
243
- if (self.options.global || self.options.g || self.options._.includes('global')) {
244
- const parentPath = `/Users/${os.userInfo().username}/.nvm/versions/node`;
245
- const versions = jetpack.list(parentPath);
246
- const currentNode = process.versions.node;
247
-
248
- const response = {};
249
-
250
- // console.log(chalk.bold.blue('NVM Global Modules'));
251
-
252
- for (var i = 0; i < versions.length; i++) {
253
- try {
254
- const parsed = semverCoerce(versions[i]);
255
- const lib = path.resolve(parentPath, `v${parsed.version}`, 'lib', 'node_modules');
256
-
257
- const modules = jetpack.list(lib);
258
-
259
- const data = [
260
- ['Package', 'package.json', 'latest'],
261
- ];
262
-
263
- const config = {
264
- columnDefault: {
265
- // width: 10,
266
- },
267
- header: {
268
- alignment: 'center',
269
- content: `Global packages for v${parsed.version}`,
270
- },
271
- }
272
- const progress = new ProgressBar.SingleBar({}, ProgressBar.Presets.shades_classic);
273
- progress.start(modules.length, 0);
274
-
275
- response[parsed.version] = {};
276
-
277
- for (var j = 0; j < modules.length; j++) {
278
- progress.update(i, 0);
279
-
280
- const mod = modules[j];
281
- const packagePath = path.resolve(lib, mod, 'package.json');
282
- try {
283
- const package = require(packagePath);
284
-
285
- const packageVersion = package.version;
286
- const latestVersion = await getLatestVersion(mod);
287
- // const isEqual = _isEqual(installedVersion, packageVersion);
288
- const isLatest = _isEqual(packageVersion, latestVersion);
289
- // const verbLocal = isEqual ? 'green' : 'yellow';
290
- const verbRemote = isLatest ? 'green' : 'red';
291
-
292
- data.push([
293
- mod,
294
- packageVersion,
295
- chalk[verbRemote](latestVersion),
296
- ]);
297
-
298
- response[parsed.version][mod] = {
299
- packageVersion: packageVersion,
300
- latestVersion: latestVersion,
301
- }
302
- } catch (e) {
303
-
304
- }
305
- }
306
-
307
- progress.stop();
308
-
309
- console.log(table(data, config));
310
-
311
- } catch (e) {
312
-
313
- }
314
- }
315
-
316
- return response;
317
- }
318
-
319
- if (self.options.clean || self.options._.includes('clean')) {
320
- const NPM_INSTALL_FLAG = self.options['no-optional'] || self.options['nooptional'] ? '--no-optional' : ''
321
- const NPM_CLEAN = `rm -fr node_modules && rm -fr package-lock.json && npm cache clean --force && npm install ${NPM_INSTALL_FLAG} && npm rb`;
322
-
323
- self.log(chalk.blue(`Running: ${NPM_CLEAN}...`));
324
-
325
- return await asyncCommand(NPM_CLEAN)
326
- .then(r => {
327
- self.log(chalk.green(`Finished cleaning`));
328
- })
329
- .catch(e => {
330
- self.log(chalk.green(`Error cleaning: ${e}`));
331
- })
332
- }
333
-
334
- if (self.options.bump || self.options._.includes('bump')) {
335
- return bump(self);
336
- }
337
-
338
- };
339
-
340
- Main.prototype.log = function () {
341
- const self = this;
342
-
343
- if (self.options.log !== false) {
344
- console.log(...arguments);
345
- }
346
- };
347
-
348
- module.exports = Main;
349
-
350
- function bump(self) {
351
- const semver = require('semver');
352
- let level = '';
353
- const version = self.proj_packageJSON.version;
354
- // let version = '3.1.0-beta.0';
355
- let newVersion = [semver.major(version), semver.minor(version), semver.patch(version)];
356
- let newVersionPost = version.split('-')[1];
357
- let newVersionString = '';
358
-
359
- if (self.options.break || self.options.breaking || self.options.major || self.options['3']) {
360
- level = 'breaking';
361
- newVersion[0]++;
362
- newVersion[1] = 0;
363
- newVersion[2] = 0;
364
- } else if (self.options.feature || self.options.features || self.options.med || self.options.medium || self.options['2']) {
365
- level = 'feature';
366
- newVersion[1]++;
367
- newVersion[2] = 0;
368
- } else {
369
- level = 'patch';
370
- newVersion[2]++;
371
- }
372
- newVersionString = newVersion.join('.') + (newVersionPost ? `-${newVersionPost}` : '');
373
-
374
- self.proj_packageJSON.version = newVersionString;
375
-
376
- jetpack.write(self.proj_packageJSONPath, self.proj_packageJSON);
377
-
378
- self.log(chalk.blue(`Bumped package.json from ${chalk.bold(version)} to ${chalk.bold(newVersionString)}`));
379
-
380
- return newVersionString;
381
- }
382
-
383
-
384
- async function asyncCommand(command) {
385
- return new Promise(function(resolve, reject) {
386
- let cmd = exec(command, function (error, stdout, stderr) {
387
- if (error) {
388
- console.error(error);
389
- return reject(error);
390
- } else {
391
- return resolve(stdout);
392
- }
393
- });
394
- });
395
- }
396
-
397
- function _coerce(v) {
398
- try {
399
- return semverCoerce(v)
400
- } catch (e) {
401
- return '0.0.0'
402
- }
403
- }
404
-
405
- function _isEqual(v1, v2) {
406
- try {
407
- return semverIsEqual(v1, v2)
408
- } catch (e) {
409
- return false;
410
- }
411
- }
412
-
413
- function _getListVersion(v) {
414
- v = v.filter(i => i !== '\n');
415
- v = v[v.length - 1]
416
- v = v.split('@');
417
- return v[v.length - 1]
418
- }
419
-
420
- function getLatestVersion(package) {
421
- return new Promise(function(resolve, reject) {
422
- const npm = new Npm();
423
-
424
- npm.repo(package)
425
- .package()
426
- .then(function(pkg) {
427
- resolve(pkg.version);
428
- }, function(err) {
429
- resolve('?');
430
- });
431
- });
432
- }