-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
·158 lines (142 loc) · 4.69 KB
/
cli.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env node
'use strict'
/* hack - fixes bug in multicodec table */
const path = require('path')
const table = require('multicodec/src/name-table')
const modpath = Object.keys(require.cache).find(k => k.endsWith(path.join('multicodec', 'src', 'name-table.js')))
require.cache[modpath].exports = { ...table, '0129': 'dag-json' }
/* end hack */
const mkdirp = require('mkdirp')
const push = require('./src/nodejs/push')
const linker = require('./src/nodejs/linker')
const storage = require('./src/nodejs/storage.js')
const deflate = require('./src/nodejs/deflate.js')
const { execSync } = require('child_process')
const printify = require('@ipld/printify')
const registry = require('./src/nodejs/registry')
const CID = require('cids')
const createTypes = require('./src/nodejs/types')
const tmp = require('tmp')
const tmpdir = f => tmp.dirSync(f)
const pushOptions = yargs => {
}
const runStage = async argv => {
const store = storage.local()
const pkg = await push(argv.filename, store.put)
const cid = await pkg.block().cid()
mkdirp.sync(argv['target-dir'])
const manifest = await deflate(cid, argv['target-dir'], store)
console.log(manifest)
console.log(`Staged "@reg/${cid.toString()}"`)
manifest.main = cid
return manifest
}
const runPublish = async argv => {
const store = storage.store(argv.token)
const pkg = await push(argv.filename, store.put)
const cid = (await pkg.block().cid()).toString()
console.log(`Published "@reg/${cid}"`)
const _registry = registry(argv.token)
const res = await _registry.alias(argv.name, cid, argv.semver, argv.latest)
console.log(`Aliased ${argv.name + '/' + argv.semver}`)
if (res.info.latest) {
console.log(`Aliased ${argv.name}`)
}
}
const runCat = async argv => {
const _registry = registry()
const pkg = await _registry.pkg(argv.name)
const store = storage.store()
const types = createTypes({ getBlock: store.get })
const block = await store.get(new CID(pkg.pkg))
const p = types.Package.decoder(block.decode())
const data = await p.getNode('*/file/data')
for await (const chunk of data.read()) {
process.stdout.write(chunk)
}
}
const bin = path.join(__dirname, 'reg.sh')
const runScript = async argv => {
const dir = tmpdir().name
argv['target-dir'] = dir
const stage = await runStage(argv)
const filename = path.join(dir, stage.main.toString('base32') + '.js')
return execSync(`${bin} ${filename}`, { stdio: 'inherit' })
}
const runLinker = async argv => {
for await (let { root, block } of linker(argv.filename)) {
if (root) {
block = root.block()
}
if (block.codec === 'raw') {
console.log('Block<raw>', (await block.cid()).toString())
} else {
console.log('Block<' + block.codec + '>', printify(block.decode()))
}
}
}
const runInfo = async argv => {
const _registry = registry()
const pkg = await _registry.pkg(argv.name)
console.log(pkg)
}
const validate = str => {
try {
new CID(str)
} catch (e) {
return false
}
return true
}
const runPkgInfo = async argv => {
let cid
if (!validate(argv.cid)) {
const _registry = registry()
const pkg = await _registry.pkg(argv.name)
cid = new CID(pkg.pkg)
} else {
cid = new CID(argv.cid)
}
const store = storage.store()
const block = await store.get(cid)
console.log(printify(block.decode()))
}
const inputOptions = yargs => {
yargs.positional('filename', {
desc: 'Filename of script to run. Example `reg myFile.js`'
})
}
const stageOptions = yargs => {
inputOptions(yargs)
yargs.option('target-dir', {
desc: 'Directory to deflate all required files',
default: path.join(process.env.HOME, '.reg', 'deflate')
})
}
const publishOptions = yargs => {
inputOptions(yargs)
yargs.option('token', {
describe: 'GitHub personal access token',
type: 'string',
default: process.env.GHTOKEN || process.env.GITHUB_TOKEN
})
yargs.positional('semver', {
describe: 'Package version number.',
type: 'string',
default: 'minor'
})
}
const yargs = require('yargs')
const args = yargs
.command('$0 <filename>', 'Run a local script file in reg', inputOptions, runScript)
.command('publish <filename> <name> <semver>',
'Publish a module to the registry', publishOptions, runPublish)
.command('stage <filename>', 'Run the linker and stage the tree in local cache', stageOptions, runStage)
.command('linker <filename>', 'Run the static linker', inputOptions, runLinker)
.command('info <name>', 'Get info for named alias', () => {}, runInfo)
.command('cat <name>', 'Print the file data for the named alias', () => {}, runCat)
.command('pkg-info <cid|name>', 'Get package information', () => {}, runPkgInfo)
.argv
if (!args._.length && !args.filename) {
yargs.showHelp()
}