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

Commit

Permalink
feat(cli): got a baseline CLI working again
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 8, 2018
1 parent 195b75c commit 09a157f
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 110 deletions.
32 changes: 18 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,42 @@

require('./lib/node/index.js')

const cp = require('child_process')
const fs = require('fs')
const npmlog = require('npmlog')
const path = require('path')
const util = require('util')
const yargs = require('yargs')

const readFileAsync = util.promisify(fs.readFile)

if (require.main === module) {
main()
}
module.exports = main
async function main () {
const startTime = Date.now()
function main () {
npmlog.heading = 'frog'
let pkgMap = await checkPkgMap()
let pkgMap = checkPkgMap()
if (!pkgMap) {
const config = await require('./lib/config.js').fromNpm(process.argv)
npmlog.level = config.loglevel
pkgMap = (await require('./lib/installer.js')(config.concat({
log (level, ...args) { return npmlog[level](...args) }
}))).pkgMap
cp.spawnSync(process.argv[0], [
require.resolve('./lib/worker.js'), ...process.argv.slice(2)
], {
stdio: 'inherit'
})
}
npmlog.info('package-map', 'package map loaded in', `${(Date.now() - startTime) / 1000}s`)
npmlog.notice('exec', 'executing `main` using frogged dependencies (TODO)')
npmlog.notice('exec', 'dep lockfile integrity:', pkgMap.lockfile_integrity)
const nodeArgs = [...([].concat(yargs.argv.nodeArg || [])), ...yargs.argv._]
cp.spawnSync(
process.argv[0],
['-r', require.resolve('./lib/node'), ...nodeArgs],
{stdio: 'inherit'}
)
}

async function checkPkgMap () {
function checkPkgMap () {
try {
const base = process.cwd()
const lock = JSON.parse(stripBOM(await readFileAsync(path.join(base, 'package-lock.json'), 'utf8')))
const map = JSON.parse(stripBOM(await readFileAsync(path.join(base, 'node_modules', '.package-map.json'), 'utf8')))
const lock = JSON.parse(stripBOM(readFileAsync(path.join(base, 'package-lock.json'), 'utf8')))
const map = JSON.parse(stripBOM(readFileAsync(path.join(base, 'node_modules', '.package-map.json'), 'utf8')))
require('ssri').checkData(
JSON.stringify(lock), map.lockfile_integrity, {error: true}
)
Expand Down
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function getNpmConfig (argv) {
})
}

return await new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
child.on('error', reject)
child.on('close', (code) => {
if (code === 127) {
Expand Down
4 changes: 4 additions & 0 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const writeFileAsync = BB.promisify(fs.writeFile)
class Installer {
constructor (opts) {
this.opts = config(opts)
this.opts = this.opts.concat({
cache: path.join(this.opts.cache, '_cacache')
})

// Stats
this.startTime = Date.now()
Expand Down Expand Up @@ -314,6 +317,7 @@ class Installer {
const lockStr = JSON.stringify(this.pkg._shrinkwrap)
const lockHash = ssri.fromData(lockStr, {algorithms: ['sha256']})
const pkgMap = {
'cache': this.opts.cache,
'lockfile_integrity': lockHash.toString(),
'path_prefix': '/node_modules'
}
Expand Down
10 changes: 4 additions & 6 deletions lib/worker.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
'use strict'

require('./node/index.js')

const npmlog = require('npmlog')

main()
async function main () {
npmlog.heading = 'frog'

const config = await require('../lib/config.js').fromNpm(process.argv)
const config = await require('./config.js').fromNpm(process.argv)
npmlog.level = config.loglevel
const pkgMap = await require('../index.js')(config.concat({
await require('./installer.js')(config.concat({
log (level, ...args) { return npmlog[level](...args) }
}))
console.log(JSON.stringify({
pkgMap,
cache: config.cache
}))
}
Loading

0 comments on commit 09a157f

Please sign in to comment.