Skip to content
This repository was archived by the owner on Jul 28, 2021. It is now read-only.

Commit 1da534e

Browse files
committed
refactor(cmds): refactoring command invocation
This makes startup much faster and lighterweight at the cost of a bit more layout complexity.
1 parent bc20c7c commit 1da534e

35 files changed

+836
-777
lines changed

bin/tink.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
#!/usr/bin/env node
22

3-
require('../lib/node/index.js')
3+
require('../lib/node/extensions.js').overrideNode()
44

5-
const CMDS = new Map([
6-
['access', require('../lib/commands/access.jsx')],
7-
['add', require('../lib/commands/add.js')],
8-
['build', require('../lib/commands/build.js')],
9-
['deprecate', require('../lib/commands/deprecate.js')],
10-
['org', require('../lib/commands/org.jsx')],
11-
['ping', require('../lib/commands/ping.js')],
12-
['prepare', require('../lib/commands/prepare.js')],
13-
['profile', require('../lib/commands/profile.jsx')],
14-
['rm', require('../lib/commands/rm.js')],
15-
['shell', require('../lib/commands/shell.js')],
16-
['team', require('../lib/commands/team.js')],
17-
['view', require('../lib/commands/view.js')],
18-
['whoami', require('../lib/commands/whoami.js')]
5+
const CMDS = new Set([
6+
'access',
7+
'add',
8+
'build',
9+
'deprecate',
10+
'org',
11+
'ping',
12+
'prepare',
13+
'profile',
14+
'rm',
15+
'shell',
16+
'team',
17+
'view',
18+
'whoami'
19+
])
20+
21+
const ALIASES = new Map([
22+
['prep', 'prepare'],
23+
['sh', 'shell']
1924
])
2025

2126
if (require.main === module) {
@@ -26,22 +31,25 @@ module.exports = main
2631
function main (argv) {
2732
const log = require('npmlog')
2833
log.heading = 'tink'
29-
const npmConfig = require('../lib/config.js')
30-
return runCommandWithYargs(argv, log, npmConfig)
34+
return runCommandWithYargs(argv, log)
3135
}
3236

33-
function runCommandWithYargs (argv, log, npmConfig) {
34-
// This code path costs ~200ms on startup.
37+
function runCommandWithYargs (argv, log) {
3538
let config = require('yargs')
3639
.demandCommand(1, 'Subcommand is required')
3740
.recommendCommands()
3841
.help()
3942
.alias('help', 'h')
4043
.alias('version', 'v')
4144
.completion()
42-
for (const mod of CMDS.values()) {
43-
config = config.command(mod)
45+
if (ALIASES.has(argv[2])) {
46+
config = config.command(require(`../lib/yargs-modules/${ALIASES.get(argv[2])}.js`))
47+
} else if (CMDS.has(argv[2])) {
48+
config = config.command(require(`../lib/yargs-modules/${argv[2]}.js`))
49+
} else {
50+
for (const mod of CMDS.values()) {
51+
config = config.command(require(`../lib/yargs-modules/${mod}.js`))
52+
}
4453
}
45-
require('../lib/node/index.js')
4654
config = config.argv
4755
}

lib/commands/access.jsx

Lines changed: 39 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,20 @@
11
'use strict'
22

3-
const Access = module.exports = {
4-
command: 'access',
5-
describe: 'access-related subcommands',
6-
builder (y) {
7-
return y.help().alias('help', 'h')
8-
.options(Access.options)
9-
.demandCommand(1, 'Access subcommand is required')
10-
.recommendCommands()
11-
.command(
12-
'public <spec>',
13-
'Set a package to be publicly accessible',
14-
Access.options,
15-
async argv => accessPublic(argv)
16-
)
17-
.command(
18-
'restricted <spec>',
19-
'Set a package to be restricted',
20-
Access.options,
21-
async argv => accessRestricted(argv)
22-
)
23-
.command(
24-
'grant <permissions> <team> <spec>',
25-
'Add the ability of users and teams to have read-only or ' +
26-
'read-write access to a package',
27-
Access.options,
28-
async argv => accessGrant(argv)
29-
)
30-
.command(
31-
'revoke <team> <spec>',
32-
'Remove the ability of users and teams to have read-only or ' +
33-
'read-write access to a package',
34-
Access.options,
35-
async argv => accessRevoke(argv)
36-
)
37-
.command(
38-
'ls-packages [<entity>]',
39-
'Show all of the packages a user or a team is able to access, along ' +
40-
'with the access level, except for read-only public packages',
41-
Access.options,
42-
async argv => accessLsPackages(argv)
43-
)
44-
.command(
45-
'ls-collaborators [<spec> [<user>]]',
46-
'Show all of the access privileges for a package. Will only show ' +
47-
'permissions for packages to which you have at least read access. ' +
48-
'If <user> is passed in, the list is filtered only to teams that ' +
49-
'user happens to belong to',
50-
Access.options,
51-
async argv => accessLsCollaborators(argv)
52-
)
53-
.command(
54-
'edit <package>',
55-
'Set the access privileges for a package at once using $EDITOR',
56-
Access.options,
57-
async argv => accessEdit(argv)
58-
)
59-
},
60-
options: Object.assign(require('../common-opts.js', {}))
61-
}
62-
633
const figgyPudding = require('figgy-pudding')
64-
const npmConfig = require('../config.js')
654
const libnpm = require('libnpm')
665

67-
const Config = figgyPudding({
6+
const ConfigOpts = figgyPudding({
7+
entity: {},
688
json: {},
699
loglevel: {},
7010
parseable: {},
71-
silent: {}
11+
permissions: {},
12+
silent: {},
13+
spec: {},
14+
team: {},
15+
user: {}
7216
})
7317

74-
const parseOpts = argv => Config(npmConfig(argv))
75-
7618
const render = (opts, content = {}) => {
7719
const { h, renderToString } = require('ink') // eslint-disable-line
7820
const Table = require('ink-table').default
@@ -92,45 +34,54 @@ const render = (opts, content = {}) => {
9234
}
9335
}
9436

95-
async function accessPublic (argv) {
96-
await libnpm.access.public(argv.spec, parseOpts(argv))
37+
module.exports.public = accessPublic
38+
async function accessPublic (argv, opts) {
39+
opts = ConfigOpts(opts)
40+
await libnpm.access.public(opts.spec, opts)
9741
}
9842

99-
async function accessRestricted (argv) {
100-
await libnpm.access.restricted(argv.spec, parseOpts(argv))
43+
module.exports.restricted = accessRestricted
44+
async function accessRestricted (argv, opts) {
45+
opts = ConfigOpts(opts)
46+
await libnpm.access.restricted(opts.spec, opts)
10147
}
10248

103-
async function accessGrant (argv) {
104-
await access.grant(argv.spec, argv.team, argv.permissions, parseOpts(argv)) // eslint-disable-line
49+
module.exports.grant = accessGrant
50+
async function accessGrant (argv, opts) {
51+
opts = ConfigOpts(opts)
52+
await access.grant(opts.spec, opts.team, opts.permissions, opts) // eslint-disable-line
10553
}
10654

107-
async function accessRevoke (argv) {
108-
await libnpm.access.revoke(argv.spec, argv.team, parseOpts(argv))
55+
module.exports.revoke = accessRevoke
56+
async function accessRevoke (argv, opts) {
57+
opts = ConfigOpts(opts)
58+
await libnpm.access.revoke(opts.spec, opts.team, opts)
10959
}
11060

111-
async function accessLsPackages (argv) {
61+
module.exports.lsPackages = accessLsPackages
62+
async function accessLsPackages (argv, opts) {
63+
opts = ConfigOpts(opts)
11264
const getPackagesByCurrentUser = async () => {
113-
const whoami = require('./whoami')
114-
return whoami.handler(parseOpts(argv).concat({ silent: true }))
65+
const whoami = require('./whoami.js')
66+
return whoami([], opts.concat({ silent: true }))
11567
}
11668

117-
const entity = argv.entity
118-
? argv.entity
69+
const entity = opts.entity
70+
? opts.entity
11971
: await getPackagesByCurrentUser()
12072

121-
const opts = parseOpts(argv)
122-
// TODO: error when opts is used as 2nd param in .lsPackages
12373
const packages =
124-
await libnpm.access.lsPackages(entity)
74+
await libnpm.access.lsPackages(entity, opts)
12575
render(opts, packages)
12676
}
12777

128-
async function accessLsCollaborators (argv) {
129-
const opts = parseOpts(argv)
78+
module.exports.lsCollaborators = accessLsCollaborators
79+
async function accessLsCollaborators (argv, opts) {
80+
opts = ConfigOpts(opts)
13081

131-
if (argv.spec) {
82+
if (opts.spec) {
13283
const collaborators =
133-
await libnpm.access.lsCollaborators(argv.spec, argv.user, opts)
84+
await libnpm.access.lsCollaborators(opts.spec, opts.user, opts)
13485
render(opts, collaborators)
13586
} else {
13687
const prefix = await libnpm.getPrefix(process.cwd())
@@ -143,13 +94,14 @@ async function accessLsCollaborators (argv) {
14394

14495
if (data) {
14596
const collaborators =
146-
await libnpm.access.lsCollaborators(data.name, argv.user, opts)
97+
await libnpm.access.lsCollaborators(data.name, opts.user, opts)
14798
render(opts, collaborators)
14899
}
149100
}
150101
}
151102
}
152103

153-
async function accessEdit (argv) {
104+
module.exports.edit = accessEdit
105+
async function accessEdit (argv, opts) {
154106
// TODO: stub
155107
}

lib/commands/add.js

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,27 @@
11
'use strict'
22

3-
const Add = module.exports = {
4-
command: 'add <pkg>',
5-
describe: 'Add a dependency.',
6-
builder (y) {
7-
return y.help().alias('help', 'h')
8-
.options(Add.options)
9-
},
10-
options: Object.assign(require('../common-opts.js', {
11-
'development': {
12-
alias: ['dev', 'D'],
13-
describe: 'Add this dependency as a devDependency',
14-
type: 'boolean'
15-
},
16-
'production': {
17-
alias: ['prod', 'P'],
18-
describe: 'Add this dependency as a regular dependency',
19-
type: 'boolean',
20-
default: true
21-
},
22-
'bundle': {
23-
alias: ['bundled', 'B'],
24-
describe: 'Add this dependency as a bundledDependency',
25-
type: 'boolean'
26-
}
27-
})),
28-
handler: async argv => add(argv)
29-
}
30-
31-
async function add (argv) {
32-
const BB = require('bluebird')
3+
const BB = require('bluebird')
334

34-
const figgyPudding = require('figgy-pudding')
35-
const npmConfig = require('../config.js')
36-
const spawn = require('child_process').spawn
5+
const figgyPudding = require('figgy-pudding')
6+
const prepare = require('./prepare.js')
7+
const spawn = require('child_process').spawn
378

38-
const AddConfig = figgyPudding({
39-
log: {},
40-
silent: {}
41-
}, { other () { return true } })
9+
const AddConfig = figgyPudding({
10+
log: {},
11+
silent: {}
12+
}, { other () { return true } })
4213

43-
const opts = AddConfig(npmConfig(argv))
14+
module.exports = add
15+
async function add (argv, opts) {
16+
opts = AddConfig(opts)
17+
const packages = argv.concat(opts.packages)
4418

4519
await new BB((resolve, reject) => {
4620
const npmBin = process.platform === 'win32' ? 'npm.cmd' : 'npm'
4721
const child = spawn(npmBin, [
48-
'add', opts.pkg, '--package-lock-only'
22+
'add', ...packages, '--package-lock-only'
4923
// We add argv here to get npm to parse those options for us :D
50-
].concat(process.argv.slice(3).filter(x => {
24+
].concat(process.argv.slice(packages.length + 3).filter(x => {
5125
return !['--bundle', '--development', '--production'].find(y => y === x)
5226
}) || []), {
5327
env: process.env,
@@ -65,5 +39,5 @@ async function add (argv) {
6539
}
6640
})
6741
})
68-
await require('./prepare.js').handler(opts)
42+
await prepare(argv, opts)
6943
}

lib/commands/build.js

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
'use strict'
2-
const Build = module.exports = {
3-
command: 'build',
4-
describe: 'Executes the configured build script, if present, or executes ' +
5-
' silently',
6-
builder (y) {
7-
return y.help().alias('help', 'h')
8-
.options(Build.options)
9-
},
10-
options: Object.assign(require('../common-opts.js', {})),
11-
handler: async argv => build(argv)
12-
}
132

14-
async function build (argv) {
15-
const libnpm = require('libnpm')
16-
const figgyPudding = require('figgy-pudding')
17-
const npmConfig = require('../config.js')
3+
const libnpm = require('libnpm')
4+
const figgyPudding = require('figgy-pudding')
5+
6+
const BuildConfig = figgyPudding({
7+
log: { default: () => require('npmlog') },
8+
dir: { default: () => process.cwd() },
9+
production: { default: false },
10+
nodeOptions: { default: {} },
11+
config: { default: {} },
12+
unsafePerm: { default: false },
13+
scriptsPrependNodePath: { default: false },
14+
ignoreScripts: { default: false },
15+
user: {},
16+
group: {},
17+
stdio: {},
18+
scriptShell: {}
19+
})
20+
21+
module.exports = build
22+
async function build (argv, opts) {
23+
opts = BuildConfig(opts)
1824

19-
const cwd = process.cwd()
20-
const BuildConfig = figgyPudding({
21-
log: {},
22-
dir: { default: cwd },
23-
production: { default: false },
24-
nodeOptions: { default: {} },
25-
config: { default: {} },
26-
unsafePerm: { default: false },
27-
scriptsPrependNodePath: { default: false },
28-
ignoreScripts: { default: false },
29-
user: {},
30-
group: {},
31-
stdio: {},
32-
scriptShell: {}
33-
})
34-
const opts = BuildConfig(npmConfig(argv))
3525
const prefix = await libnpm.getPrefix(process.cwd())
3626
const packageJSON = `${prefix}/package.json`
3727
const pkg = await libnpm.readJSON(packageJSON)
3828

39-
await libnpm.runScript(pkg, 'build', cwd, opts)
29+
await libnpm.runScript(pkg, 'build', opts.dir, opts)
4030
return true
4131
}

0 commit comments

Comments
 (0)