-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·112 lines (100 loc) · 3.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
const chalk = require('chalk');
const { Command } = require('commander');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const program = new Command();
program.version('0.0.1');
const adapter = new FileSync('parallel.json')
const db = low(adapter)
// Set some defaults (required if your JSON file is empty)
db.defaults({ packages: {}, installed: [] })
.write();
const success = chalk.green('success');
const warning = chalk.yellow('warning');
const error = chalk.red('error');
const info = chalk.blue('info');
const memoGetDependencies = async (dependencies) => {
const packages = await db.get('packages').toJSON();
const cache = {};
const getDependencies = (depends = []) => {
return depends.reduce((all, package) => {
if (!cache[package]) {
cache[package] = true;
all.push(package);
const nextDepends = packages[package] || [];
return [...all, ...getDependencies(nextDepends)];
}
return all;
}, [])
};
return getDependencies(dependencies);
}
program
.command('depends <name>')
.option('-d, --dependencies [dependencies...]', 'dependencies', [])
.description('Sets the dependencies of a package')
.action((name, opts) => {
// Set a dependencies using Lodash shorthand syntax
console.log({ name, dependencies: opts.dependencies})
db.set(`packages.${name}`, opts.dependencies || [])
.write();
console.log(`
${warning} parallel.json found. Your project contains parallel file generated.
${success} Saved parallel.json file.
${success} Saved ${opts.dependencies.length + 1} new dependencies.
${info} Direct dependencies
└─ ${name}
${info} All dependencies
${opts.dependencies.map(d => `├─ ${d} \n`).join('')}└─ ${name}
✨ Done!`)
})
program
.command('install <name>')
.description('Install the specified package and any necessary transient dependencies.')
.action((name) => {
const hasPackage = db.get('packages').has(name).toJSON();
if(!hasPackage){
console.log(`${error} a package called "${chalk.green(name)}" does not exist. ${chalk.red('This command has had no effect.')}`);
return;
}
const hasItem = !(db.get('installed').findIndex(p => p === name).toJSON() == -1);
if(hasItem){
console.log(`${warning} There's already a package called "${chalk.green(name)}" installed. ${chalk.red('This command has had no effect.')}`);
} else {
// Add a dependencies
db.get('installed')
.push(name)
.write()
console.log(`${success} ${chalk.blue(name)} installed`);
}
});
program
.command('uninstall <name>')
.description('Uninstall the specified package and any transient dependencies that are not dependent of other installed packages.')
.action((name) => {
// remove installed package
db.get('installed')
.remove( p => p === name)
.write()
console.log(`${success} ${chalk.blue(name)} uninstalled`);
});
program
.option('-o, --order-by <type>', 'order by')
.command('list')
.description('Lists all the installed packages, including transient dependencies.')
.action(async (opts) => {
const dependencies = await db.get('installed').toJSON();
const all = await memoGetDependencies(dependencies);
if(!all.length){
console.log(chalk.blue(`${success} There are not packages installed`));
} else {
console.log(`${success} dependencies:
${
all.map((d, index) => `${all.length - 1 !== index ? '├─' : '└─'} ${d} \n`).join('')
}
✨ Done!`)
}
});
program.parse(process.argv);
// console.log(chalk.blue('Hello world!'));