Skip to content

Commit 9e0ed67

Browse files
committed
feat: add package update dependencies command
1 parent 48ff669 commit 9e0ed67

1 file changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import ThreadsPoolQueue from "#core/threads/pool/queue";
2+
import Command from "#lib/command";
3+
4+
export default class extends Command {
5+
6+
// static
7+
static cli () {
8+
return {
9+
"options": {
10+
"direct-dependencies": {
11+
"short": "1",
12+
"description": "check only the current package direct dependencies",
13+
"default": false,
14+
"schema": {
15+
"type": "boolean",
16+
},
17+
},
18+
"outdated": {
19+
"negatedShort": "O",
20+
"description": "exclude outdated dependencies",
21+
"default": true,
22+
"schema": {
23+
"type": "boolean",
24+
},
25+
},
26+
"install": {
27+
"short": "i",
28+
"description": "install found updatable dependencies",
29+
"default": false,
30+
"schema": {
31+
"type": "boolean",
32+
},
33+
},
34+
"reinstall": {
35+
"short": "I",
36+
"description": "reinstall all dependencies, even if no updates available",
37+
"default": false,
38+
"schema": {
39+
"type": "boolean",
40+
},
41+
},
42+
"commit": {
43+
"negatedShort": "C",
44+
"description": "do not commit and push changes",
45+
"default": true,
46+
"schema": {
47+
"type": "boolean",
48+
},
49+
},
50+
"sub-packages": {
51+
"negatedShort": "S",
52+
"description": "ignore sub-packages",
53+
"default": true,
54+
"schema": { "type": "boolean" },
55+
},
56+
"quiet": {
57+
"short": "q",
58+
"description": "do not show report",
59+
"default": false,
60+
"schema": {
61+
"type": "boolean",
62+
},
63+
},
64+
"confirm": {
65+
"short": "Y",
66+
"description": "ask before install dependencies",
67+
"default": false,
68+
"schema": {
69+
"type": "boolean",
70+
},
71+
},
72+
},
73+
"arguments": {
74+
"pattern": {
75+
"description": "Filter packages using glob patterns.",
76+
"schema": { "type": "array", "items": { "type": "string", "format": "glob-pattern" } },
77+
},
78+
},
79+
};
80+
}
81+
82+
// public
83+
async run () {
84+
var res = this._findWorkspacePackages( {
85+
"patterns": process.cli.arguments?.pattern,
86+
} );
87+
if ( !res.ok ) return res;
88+
89+
var hasErrors,
90+
cache = {};
91+
92+
const packages = res.data,
93+
threads = new ThreadsPoolQueue( {
94+
"maxRunningThreads": 5,
95+
} );
96+
97+
for ( const pkg of packages ) {
98+
threads.pushThread( async () => {
99+
const res = await pkg.getOutdatedDependencies( {
100+
"all": !process.cli.options[ "direct-dependencies" ],
101+
} );
102+
103+
return result( res, {
104+
pkg,
105+
"dependencies": res.data,
106+
} );
107+
} );
108+
109+
if ( process.cli.options[ "sub-packages" ] ) {
110+
for ( const subPkg of pkg.subPackages ) {
111+
threads.pushThread( async () => {
112+
const res = await subPkg.getOutdatedDependencies( {
113+
"all": !process.cli.options[ "direct-dependencies" ],
114+
} );
115+
116+
return result( res, {
117+
"pkg": subPkg,
118+
"dependencies": res.data,
119+
} );
120+
} );
121+
}
122+
}
123+
}
124+
125+
while ( ( res = await threads.getResult() ) ) {
126+
if ( res.ok ) {
127+
const pkg = res.data.pkg;
128+
129+
res = await pkg.updateDependencies( {
130+
"all": !process.cli.options[ "direct-dependencies" ],
131+
"outdated": process.cli.options.outdated,
132+
"notInstalled": process.cli.options[ "not-installed" ],
133+
"install": process.cli.options.install,
134+
"reinstall": process.cli.options.reinstall,
135+
"commit": process.cli.options.commit,
136+
"quiet": process.cli.options.quiet,
137+
"confirmInstall": process.cli.options.confirm,
138+
"outdatedDependencies": res.data.dependencies,
139+
cache,
140+
} );
141+
}
142+
143+
if ( !res.ok ) {
144+
hasErrors = true;
145+
}
146+
}
147+
148+
if ( hasErrors ) {
149+
return result( [ 500, "Some dependencies wasn't updated" ] );
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)