diff --git a/lib/commands/init.js b/lib/commands/init.js index 55c9b85a0c65b..5112004e9d2db 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -160,24 +160,20 @@ class Init extends BaseCommand { ].join('\n')) } - // XXX promisify init-package-json - await new Promise((res, rej) => { - initJson(path, initFile, this.npm.config, (er, data) => { - log.resume() - log.enableProgress() - log.silly('package data', data) - if (er && er.message === 'canceled') { - log.warn('init', 'canceled') - return res() - } - if (er) { - rej(er) - } else { - log.info('init', 'written successfully') - res(data) - } - }) - }) + try { + const data = await initJson(path, initFile, this.npm.config) + log.silly('package data', data) + return data + } catch (er) { + if (er.message === 'canceled') { + log.warn('init', 'canceled') + } else { + throw er + } + } finally { + log.resume() + log.enableProgress() + } } async setWorkspace (pkg, workspacePath) { diff --git a/lib/utils/read-user-info.js b/lib/utils/read-user-info.js index 26d5b36d55b58..576816e4fe7e9 100644 --- a/lib/utils/read-user-info.js +++ b/lib/utils/read-user-info.js @@ -1,5 +1,4 @@ -const { promisify } = require('util') -const readAsync = promisify(require('read')) +const readAsync = require('read') const userValidate = require('npm-user-validate') const log = require('./log-shim.js') diff --git a/node_modules/init-package-json/lib/default-input.js b/node_modules/init-package-json/lib/default-input.js index fe5abfdd85e45..490e83c139887 100644 --- a/node_modules/init-package-json/lib/default-input.js +++ b/node_modules/init-package-json/lib/default-input.js @@ -1,190 +1,160 @@ -/* eslint-disable no-undef */ -var fs = require('fs') -var path = require('path') -var validateLicense = require('validate-npm-package-license') -var validateName = require('validate-npm-package-name') -var npa = require('npm-package-arg') -var semver = require('semver') +/* globals config, dirname, package, basename, yes, prompt */ + +const fs = require('fs/promises') +const path = require('path') +const validateLicense = require('validate-npm-package-license') +const validateName = require('validate-npm-package-name') +const npa = require('npm-package-arg') +const semver = require('semver') // more popular packages should go here, maybe? -function isTestPkg (p) { - return !!p.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/) -} +const isTestPkg = (p) => !!p.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/) -function niceName (n) { - return n.replace(/^node-|[.-]js$/g, '').replace(/\s+/g, ' ').replace(/ /g, '-').toLowerCase() -} +const invalid = (msg) => Object.assign(new Error(msg), { notValid: true }) -function readDeps (test, excluded) { - return function (cb) { - fs.readdir('node_modules', function (readdirErr, dir) { - if (readdirErr) { - return cb() - } - var deps = {} - var n = dir.length - if (n === 0) { - return cb(null, deps) - } - dir.forEach(function (d) { - if (d.match(/^\./)) { - return next() - } - if (test !== isTestPkg(d) || excluded[d]) { - return next() - } +const readDeps = (test, excluded) => async () => { + const dirs = await fs.readdir('node_modules').catch(() => null) - var dp = path.join(dirname, 'node_modules', d, 'package.json') - fs.readFile(dp, 'utf8', function (readFileErr, p) { - if (readFileErr) { - return next() - } - try { - p = JSON.parse(p) - } catch (e) { - return next() - } - if (!p.version) { - return next() - } - if (p._requiredBy) { - if (!p._requiredBy.some(function (req) { - return req === '#USER' - })) { - return next() - } - } - deps[d] = config.get('save-exact') ? p.version : config.get('save-prefix') + p.version - return next() - }) - }) - function next () { - if (--n === 0) { - return cb(null, deps) - } - } - }) + if (!dirs) { + return } + + const deps = {} + for (const dir of dirs) { + if (dir.match(/^\./) || test !== isTestPkg(dir) || excluded[dir]) { + continue + } + + const dp = path.join(dirname, 'node_modules', dir, 'package.json') + const p = await fs.readFile(dp, 'utf8').then((d) => JSON.parse(d)).catch(() => null) + + if (!p || !p.version || p?._requiredBy?.some((r) => r === '#USER')) { + continue + } + + deps[dir] = config.get('save-exact') ? p.version : config.get('save-prefix') + p.version + } + + return deps } -var name = niceName(package.name || basename) -var spec -try { - spec = npa(name) -} catch (e) { - spec = {} +const getConfig = (key) => { + // dots take precedence over dashes + const def = config?.defaults?.[`init.${key}`] + const val = config.get(`init.${key}`) + return (val !== def && val) ? val : config.get(`init-${key.replace(/\./g, '-')}`) } -var scope = config.get('scope') -if (scope) { - if (scope.charAt(0) !== '@') { - scope = '@' + scope + +const getName = () => { + const rawName = package.name || basename + let name = rawName + .replace(/^node-|[.-]js$/g, '') + .replace(/\s+/g, ' ') + .replace(/ /g, '-') + .toLowerCase() + + let spec + try { + spec = npa(name) + } catch { + spec = {} } - if (spec.scope) { - name = scope + '/' + spec.name.split('/')[1] - } else { - name = scope + '/' + name + + let scope = config.get('scope') + + if (scope) { + if (scope.charAt(0) !== '@') { + scope = '@' + scope + } + if (spec.scope) { + name = scope + '/' + spec.name.split('/')[1] + } else { + name = scope + '/' + name + } } + + return name } -exports.name = yes ? name : prompt('package name', name, function (data) { - var its = validateName(data) + +const name = getName() +exports.name = yes ? name : prompt('package name', name, (data) => { + const its = validateName(data) if (its.validForNewPackages) { return data } - var errors = (its.errors || []).concat(its.warnings || []) - var er = new Error('Sorry, ' + errors.join(' and ') + '.') - er.notValid = true - return er + const errors = (its.errors || []).concat(its.warnings || []) + return invalid(`Sorry, ${errors.join(' and ')}.`) }) -const defaultDottedInitVersion = config && - config.defaults && - config.defaults['init.version'] -const dottedInitVersion = - config.get('init.version') !== defaultDottedInitVersion && - config.get('init.version') -var version = package.version || - dottedInitVersion || - config.get('init-version') || - '1.0.0' -exports.version = yes ? - version : - prompt('version', version, function (promptedVersion) { - if (semver.valid(promptedVersion)) { - return promptedVersion - } - var er = new Error('Invalid version: "' + promptedVersion + '"') - er.notValid = true - return er - }) +const version = package.version || getConfig('version') || '1.0.0' +exports.version = yes ? version : prompt('version', version, (v) => { + if (semver.valid(v)) { + return v + } + return invalid(`Invalid version: "${v}"`) +}) if (!package.description) { exports.description = yes ? '' : prompt('description') } if (!package.main) { - exports.main = function (cb) { - fs.readdir(dirname, function (er, f) { - if (er) { - f = [] - } + exports.main = async () => { + const files = await fs.readdir(dirname) + .then(list => list.filter((f) => f.match(/\.js$/))) + .catch(() => []) - f = f.filter(function (filtered) { - return filtered.match(/\.js$/) - }) - - if (f.indexOf('index.js') !== -1) { - f = 'index.js' - } else if (f.indexOf('main.js') !== -1) { - f = 'main.js' - } else if (f.indexOf(basename + '.js') !== -1) { - f = basename + '.js' - } else { - f = f[0] - } + let index + if (files.includes('index.js')) { + index = 'index.js' + } else if (files.includes('main.js')) { + index = 'main.js' + } else if (files.includes(basename + '.js')) { + index = basename + '.js' + } else { + index = files[0] || 'index.js' + } - var index = f || 'index.js' - return cb(null, yes ? index : prompt('entry point', index)) - }) + return yes ? index : prompt('entry point', index) } } if (!package.bin) { - exports.bin = function (cb) { - fs.readdir(path.resolve(dirname, 'bin'), function (er, d) { - // no bins - if (er) { - return cb() - } + exports.bin = async () => { + try { + const d = await fs.readdir(path.resolve(dirname, 'bin')) // just take the first js file we find there, or nada let r = d.find(f => f.match(/\.js$/)) if (r) { r = `bin/${r}` } - return cb(null, r) - }) + return r + } catch { + // no bins + } } } -exports.directories = function (cb) { - fs.readdir(dirname, function (er, dirs) { - if (er) { - return cb(er) - } - var res = {} - dirs.forEach(function (d) { - switch (d) { - case 'example': case 'examples': return res.example = d - case 'test': case 'tests': return res.test = d - case 'doc': case 'docs': return res.doc = d - case 'man': return res.man = d - case 'lib': return res.lib = d - } - }) - if (Object.keys(res).length === 0) { - res = undefined +exports.directories = async () => { + const dirs = await fs.readdir(dirname) + + const res = dirs.reduce((acc, d) => { + if (/^examples?$/.test(d)) { + acc.example = d + } else if (/^tests?$/.test(d)) { + acc.test = d + } else if (/^docs?$/.test(d)) { + acc.doc = d + } else if (d === 'man') { + acc.man = d + } else if (d === 'lib') { + acc.lib = d } - return cb(null, res) - }) + + return acc + }, {}) + + return Object.keys(res).length === 0 ? undefined : res } if (!package.dependencies) { @@ -196,116 +166,97 @@ if (!package.devDependencies) { } // MUST have a test script! -var s = package.scripts || {} -var notest = 'echo "Error: no test specified" && exit 1' if (!package.scripts) { - exports.scripts = function (cb) { - fs.readdir(path.join(dirname, 'node_modules'), function (er, d) { - setupScripts(d || [], cb) - }) - } -} -function setupScripts (d, cb) { - // check to see what framework is in use, if any - function tx (test) { - return test || notest - } - if (!s.test || s.test === notest) { - var commands = { - tap: 'tap test/*.js', - expresso: 'expresso test', - mocha: 'mocha', - } - var command - Object.keys(commands).forEach(function (k) { - if (d.indexOf(k) !== -1) { - command = commands[k] + const scripts = package.scripts || {} + const notest = 'echo "Error: no test specified" && exit 1' + exports.scripts = async () => { + const d = await fs.readdir(path.join(dirname, 'node_modules')).catch(() => []) + + // check to see what framework is in use, if any + let command + if (!scripts.test || scripts.test === notest) { + const commands = { + tap: 'tap test/*.js', + expresso: 'expresso test', + mocha: 'mocha', + } + for (const [k, v] of Object.entries(commands)) { + if (d.includes(k)) { + command = v + } } - }) - var ps = 'test command' - if (yes) { - s.test = command || notest - } else { - s.test = command ? prompt(ps, command, tx) : prompt(ps, tx) } + + const promptArgs = ['test command', (t) => t || notest] + if (command) { + promptArgs.splice(1, 0, command) + } + scripts.test = yes ? command || notest : prompt(...promptArgs) + + return scripts } - return cb(null, s) } if (!package.repository) { - exports.repository = function (cb) { - fs.readFile('.git/config', 'utf8', function (er, gconf) { - if (er || !gconf) { - return cb(null, yes ? '' : prompt('git repository')) - } - gconf = gconf.split(/\r?\n/) - var i = gconf.indexOf('[remote "origin"]') - if (i !== -1) { - var u = gconf[i + 1] - if (!u.match(/^\s*url =/)) { - u = gconf[i + 2] - } - if (!u.match(/^\s*url =/)) { - u = null - } else { - u = u.replace(/^\s*url = /, '') - } + exports.repository = async () => { + const gconf = await fs.readFile('.git/config', 'utf8').catch(() => '') + const lines = gconf.split(/\r?\n/) + + let url + const i = lines.indexOf('[remote "origin"]') + + if (i !== -1) { + url = gconf[i + 1] + if (!url.match(/^\s*url =/)) { + url = gconf[i + 2] } - if (u && u.match(/^git@github.com:/)) { - u = u.replace(/^git@github.com:/, 'https://github.com/') + if (!url.match(/^\s*url =/)) { + url = null + } else { + url = url.replace(/^\s*url = /, '') } + } + + if (url && url.match(/^git@github.com:/)) { + url = url.replace(/^git@github.com:/, 'https://github.com/') + } - return cb(null, yes ? u : prompt('git repository', u)) - }) + return yes ? url || '' : prompt('git repository', url || undefined) } } if (!package.keywords) { - exports.keywords = yes ? '' : prompt('keywords', function (promptedKeywords) { - if (!promptedKeywords) { - return undefined + exports.keywords = yes ? '' : prompt('keywords', (data) => { + if (!data) { + return } - if (Array.isArray(promptedKeywords)) { - promptedKeywords = promptedKeywords.join(' ') + if (Array.isArray(data)) { + data = data.join(' ') } - if (typeof promptedKeywords !== 'string') { - return promptedKeywords + if (typeof data !== 'string') { + return data } - return promptedKeywords.split(/[\s,]+/) + return data.split(/[\s,]+/) }) } if (!package.author) { - exports.author = config.get('init.author.name') || - config.get('init-author-name') + const authorName = getConfig('author.name') + exports.author = authorName ? { - name: config.get('init.author.name') || - config.get('init-author-name'), - email: config.get('init.author.email') || - config.get('init-author-email'), - url: config.get('init.author.url') || - config.get('init-author-url'), + name: authorName, + email: getConfig('author.email'), + url: getConfig('author.url'), } : yes ? '' : prompt('author') } -const defaultDottedInitLicense = config && - config.defaults && - config.defaults['init.license'] -const dottedInitLicense = - config.get('init.license') !== defaultDottedInitLicense && - config.get('init.license') -var license = package.license || - dottedInitLicense || - config.get('init-license') || - 'ISC' -exports.license = yes ? license : prompt('license', license, function (data) { - var its = validateLicense(data) +const license = package.license || getConfig('license') || 'ISC' +exports.license = yes ? license : prompt('license', license, (data) => { + const its = validateLicense(data) if (its.validForNewPackages) { return data } - var errors = (its.errors || []).concat(its.warnings || []) - var er = new Error('Sorry, ' + errors.join(' and ') + '.') - er.notValid = true - return er + const errors = (its.errors || []).concat(its.warnings || []) + return invalid(`Sorry, ${errors.join(' and ')}.`) }) diff --git a/node_modules/init-package-json/lib/init-package-json.js b/node_modules/init-package-json/lib/init-package-json.js index 230bcd81747bd..077ebd96ffc52 100644 --- a/node_modules/init-package-json/lib/init-package-json.js +++ b/node_modules/init-package-json/lib/init-package-json.js @@ -1,184 +1,145 @@ -module.exports = init -module.exports.yes = yes - -var PZ = require('promzard').PromZard -var path = require('path') -var def = require.resolve('./default-input.js') +const promzard = require('promzard') +const path = require('path') +const fs = require('fs/promises') +const semver = require('semver') +const read = require('read') +const util = require('util') +const rpj = require('read-package-json') -var fs = require('fs') -var semver = require('semver') -var read = require('read') +const def = require.resolve('./default-input.js') // to validate the data object at the end as a worthwhile package // and assign default values for things. -var readJson = require('read-package-json') +const _extraSet = rpj.extraSet +const _rpj = util.promisify(rpj) +const _rpjExtras = util.promisify(rpj.extras) +const readPkgJson = async (file, pkg) => { + // only do a few of these. no need for mans or contributors if they're in the files + rpj.extraSet = _extraSet.filter(f => f.name !== 'authors' && f.name !== 'mans') + const p = pkg ? _rpjExtras(file, pkg) : _rpj(file) + return p.catch(() => ({})).finally(() => rpj.extraSet = _extraSet) +} + +const isYes = (c) => !!(c.get('yes') || c.get('y') || c.get('force') || c.get('f')) + +const getConfig = (c = {}) => { + // accept either a plain-jane object, or a config object with a "get" method. + if (typeof c.get !== 'function') { + const data = c + return { + get: (k) => data[k], + toJSON: () => data, + } + } + return c +} -function yes (conf) { - return !!( - conf.get('yes') || conf.get('y') || - conf.get('force') || conf.get('f') - ) +const stringifyPerson = (p) => { + if (typeof p === 'string') { + return p + } + const { name = '', url, web, email, mail } = p + const u = url || web + const e = email || mail + return `${name}${e ? ` <${e}>` : ''}${u ? ` (${u})` : ''}` } -function init (dir, input, config, cb) { - if (typeof config === 'function') { - cb = config - config = {} +async function init (dir, input = def, c = {}) { + const config = getConfig(c) + const yes = isYes(config) + const packageFile = path.resolve(dir, 'package.json') + + const pkg = await readPkgJson(packageFile) + + if (!semver.valid(pkg.version)) { + delete pkg.version } - // accept either a plain-jane object, or a config object - // with a "get" method. - if (typeof config.get !== 'function') { - var data = config - config = { - get: function (k) { - return data[k] - }, - toJSON: function () { - return data - }, + // make sure that the input is valid. if not, use the default + const pzData = await promzard(path.resolve(input), { + yes, + config, + filename: packageFile, + dirname: path.dirname(packageFile), + basename: path.basename(path.dirname(packageFile)), + package: pkg, + }, { backupFile: def }) + + for (const [k, v] of Object.entries(pzData)) { + if (v != null) { + pkg[k] = v } } - var packageFile = path.resolve(dir, 'package.json') - input = path.resolve(input) - var pkg - var ctx = { yes: yes(config) } - - var es = readJson.extraSet - readJson.extraSet = es.filter(function (fn) { - return fn.name !== 'authors' && fn.name !== 'mans' - }) - readJson(packageFile, function (er, d) { - readJson.extraSet = es - - if (er) { - pkg = {} - } else { - pkg = d - } + const pkgExtras = await readPkgJson(packageFile, pkg) + + // turn the objects into somewhat more humane strings. + if (pkgExtras.author) { + pkgExtras.author = stringifyPerson(pkgExtras.author) + } - ctx.filename = packageFile - ctx.dirname = path.dirname(packageFile) - ctx.basename = path.basename(ctx.dirname) - if (!pkg.version || !semver.valid(pkg.version)) { - delete pkg.version + for (const set of ['maintainers', 'contributors']) { + if (Array.isArray(pkgExtras[set])) { + pkgExtras[set] = pkgExtras[set].map(stringifyPerson) } + } - ctx.package = pkg - ctx.config = config || {} - - // make sure that the input is valid. - // if not, use the default - var pz = new PZ(input, ctx) - pz.backupFile = def - pz.on('error', cb) - pz.on('data', function (pzData) { - Object.keys(pzData).forEach(function (k) { - if (pzData[k] !== undefined && pzData[k] !== null) { - pkg[k] = pzData[k] - } - }) - - // only do a few of these. - // no need for mans or contributors if they're in the files - es = readJson.extraSet - readJson.extraSet = es.filter(function (fn) { - return fn.name !== 'authors' && fn.name !== 'mans' - }) - readJson.extras(packageFile, pkg, function (extrasErr, pkgWithExtras) { - if (extrasErr) { - return cb(extrasErr, pkgWithExtras) - } - readJson.extraSet = es - pkgWithExtras = unParsePeople(pkgWithExtras) - // no need for the readme now. - delete pkgWithExtras.readme - delete pkgWithExtras.readmeFilename - - // really don't want to have this lying around in the file - delete pkgWithExtras._id - - // ditto - delete pkgWithExtras.gitHead - - // if the repo is empty, remove it. - if (!pkgWithExtras.repository) { - delete pkgWithExtras.repository - } - - // readJson filters out empty descriptions, but init-package-json - // traditionally leaves them alone - if (!pkgWithExtras.description) { - pkgWithExtras.description = pzData.description - } - - var stringified = JSON.stringify(updateDeps(pkgWithExtras), null, 2) + '\n' - function write (writeYes) { - fs.writeFile(packageFile, stringified, 'utf8', function (writeFileErr) { - if (!writeFileErr && writeYes && !config.get('silent')) { - console.log('Wrote to %s:\n\n%s\n', packageFile, stringified) - } - return cb(writeFileErr, pkgWithExtras) - }) - } - if (ctx.yes) { - return write(true) - } - console.log('About to write to %s:\n\n%s\n', packageFile, stringified) - read({ prompt: 'Is this OK? ', default: 'yes' }, function (promptErr, ok) { - if (promptErr) { - return cb(promptErr) - } - if (!ok || ok.toLowerCase().charAt(0) !== 'y') { - console.log('Aborted.') - } else { - return write() - } - }) - }) - }) - }) -} + // no need for the readme now. + delete pkgExtras.readme + delete pkgExtras.readmeFilename + + // really don't want to have this lying around in the file + delete pkgExtras._id + + // ditto + delete pkgExtras.gitHead + + // if the repo is empty, remove it. + if (!pkgExtras.repository) { + delete pkgExtras.repository + } + + // readJson filters out empty descriptions, but init-package-json + // traditionally leaves them alone + if (!pkgExtras.description) { + pkgExtras.description = pzData.description + } -function updateDeps (depsData) { // optionalDependencies don't need to be repeated in two places - if (depsData.dependencies) { - if (depsData.optionalDependencies) { - for (const name of Object.keys(depsData.optionalDependencies)) { - delete depsData.dependencies[name] + if (pkgExtras.dependencies) { + if (pkgExtras.optionalDependencies) { + for (const name of Object.keys(pkgExtras.optionalDependencies)) { + delete pkgExtras.dependencies[name] } } - if (Object.keys(depsData.dependencies).length === 0) { - delete depsData.dependencies + if (Object.keys(pkgExtras.dependencies).length === 0) { + delete pkgExtras.dependencies } } - return depsData -} + const stringified = JSON.stringify(pkgExtras, null, 2) + '\n' + const msg = util.format('%s:\n\n%s\n', packageFile, stringified) + const write = () => fs.writeFile(packageFile, stringified, 'utf8') -// turn the objects into somewhat more humane strings. -function unParsePeople (data) { - if (data.author) { - data.author = unParsePerson(data.author) - }['maintainers', 'contributors'].forEach(function (set) { - if (!Array.isArray(data[set])) { - return + if (yes) { + await write() + if (!config.get('silent')) { + console.log(`Wrote to ${msg}`) } - data[set] = data[set].map(unParsePerson) - }) - return data -} + return pkgExtras + } -function unParsePerson (person) { - if (typeof person === 'string') { - return person + console.log(`About to write to ${msg}`) + const ok = await read({ prompt: 'Is this OK? ', default: 'yes' }) + if (!ok || !ok.toLowerCase().startsWith('y')) { + console.log('Aborted.') + return } - var name = person.name || '' - var u = person.url || person.web - var url = u ? (' (' + u + ')') : '' - var e = person.email || person.mail - var email = e ? (' <' + e + '>') : '' - return name + email + url + + await write() + return pkgExtras } + +module.exports = init +module.exports.yes = isYes diff --git a/node_modules/init-package-json/package.json b/node_modules/init-package-json/package.json index 6641323e9f9c0..ffcf007e6690f 100644 --- a/node_modules/init-package-json/package.json +++ b/node_modules/init-package-json/package.json @@ -20,8 +20,8 @@ "description": "A node module to get your node module started", "dependencies": { "npm-package-arg": "^10.0.0", - "promzard": "^0.3.0", - "read": "^1.0.7", + "promzard": "^1.0.0", + "read": "^2.0.0", "read-package-json": "^6.0.0", "semver": "^7.3.5", "validate-npm-package-license": "^3.0.4", @@ -30,16 +30,18 @@ "devDependencies": { "@npmcli/config": "^6.0.0", "@npmcli/eslint-config": "^4.0.0", - "@npmcli/template-oss": "4.6.1", + "@npmcli/template-oss": "4.11.0", "tap": "^16.0.1" }, "engines": { "node": "^14.17.0 || ^16.13.0 || >=18.0.0" }, "tap": { - "statements": "94", - "branches": "83", - "lines": "94", + "statements": 95, + "branches": 78, + "lines": 94, + "jobs": 1, + "test-ignore": "fixtures/", "nyc-arg": [ "--exclude", "tap-snapshots/**" @@ -61,6 +63,6 @@ ], "templateOSS": { "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", - "version": "4.6.1" + "version": "4.11.0" } } diff --git a/node_modules/mute-stream/lib/index.js b/node_modules/mute-stream/lib/index.js new file mode 100644 index 0000000000000..368f727e2c3ed --- /dev/null +++ b/node_modules/mute-stream/lib/index.js @@ -0,0 +1,142 @@ +const Stream = require('stream') + +class MuteStream extends Stream { + #isTTY = null + + constructor (opts = {}) { + super(opts) + this.writable = this.readable = true + this.muted = false + this.on('pipe', this._onpipe) + this.replace = opts.replace + + // For readline-type situations + // This much at the start of a line being redrawn after a ctrl char + // is seen (such as backspace) won't be redrawn as the replacement + this._prompt = opts.prompt || null + this._hadControl = false + } + + #destSrc (key, def) { + if (this._dest) { + return this._dest[key] + } + if (this._src) { + return this._src[key] + } + return def + } + + #proxy (method, ...args) { + if (typeof this._dest?.[method] === 'function') { + this._dest[method](...args) + } + if (typeof this._src?.[method] === 'function') { + this._src[method](...args) + } + } + + get isTTY () { + if (this.#isTTY !== null) { + return this.#isTTY + } + return this.#destSrc('isTTY', false) + } + + // basically just get replace the getter/setter with a regular value + set isTTY (val) { + this.#isTTY = val + } + + get rows () { + return this.#destSrc('rows') + } + + get columns () { + return this.#destSrc('columns') + } + + mute () { + this.muted = true + } + + unmute () { + this.muted = false + } + + _onpipe (src) { + this._src = src + } + + pipe (dest, options) { + this._dest = dest + return super.pipe(dest, options) + } + + pause () { + if (this._src) { + return this._src.pause() + } + } + + resume () { + if (this._src) { + return this._src.resume() + } + } + + write (c) { + if (this.muted) { + if (!this.replace) { + return true + } + // eslint-disable-next-line no-control-regex + if (c.match(/^\u001b/)) { + if (c.indexOf(this._prompt) === 0) { + c = c.slice(this._prompt.length) + c = c.replace(/./g, this.replace) + c = this._prompt + c + } + this._hadControl = true + return this.emit('data', c) + } else { + if (this._prompt && this._hadControl && + c.indexOf(this._prompt) === 0) { + this._hadControl = false + this.emit('data', this._prompt) + c = c.slice(this._prompt.length) + } + c = c.toString().replace(/./g, this.replace) + } + } + this.emit('data', c) + } + + end (c) { + if (this.muted) { + if (c && this.replace) { + c = c.toString().replace(/./g, this.replace) + } else { + c = null + } + } + if (c) { + this.emit('data', c) + } + this.emit('end') + } + + destroy (...args) { + return this.#proxy('destroy', ...args) + } + + destroySoon (...args) { + return this.#proxy('destroySoon', ...args) + } + + close (...args) { + return this.#proxy('close', ...args) + } +} + +module.exports = MuteStream diff --git a/node_modules/mute-stream/mute.js b/node_modules/mute-stream/mute.js deleted file mode 100644 index a24fc09975bb3..0000000000000 --- a/node_modules/mute-stream/mute.js +++ /dev/null @@ -1,145 +0,0 @@ -var Stream = require('stream') - -module.exports = MuteStream - -// var out = new MuteStream(process.stdout) -// argument auto-pipes -function MuteStream (opts) { - Stream.apply(this) - opts = opts || {} - this.writable = this.readable = true - this.muted = false - this.on('pipe', this._onpipe) - this.replace = opts.replace - - // For readline-type situations - // This much at the start of a line being redrawn after a ctrl char - // is seen (such as backspace) won't be redrawn as the replacement - this._prompt = opts.prompt || null - this._hadControl = false -} - -MuteStream.prototype = Object.create(Stream.prototype) - -Object.defineProperty(MuteStream.prototype, 'constructor', { - value: MuteStream, - enumerable: false -}) - -MuteStream.prototype.mute = function () { - this.muted = true -} - -MuteStream.prototype.unmute = function () { - this.muted = false -} - -Object.defineProperty(MuteStream.prototype, '_onpipe', { - value: onPipe, - enumerable: false, - writable: true, - configurable: true -}) - -function onPipe (src) { - this._src = src -} - -Object.defineProperty(MuteStream.prototype, 'isTTY', { - get: getIsTTY, - set: setIsTTY, - enumerable: true, - configurable: true -}) - -function getIsTTY () { - return( (this._dest) ? this._dest.isTTY - : (this._src) ? this._src.isTTY - : false - ) -} - -// basically just get replace the getter/setter with a regular value -function setIsTTY (isTTY) { - Object.defineProperty(this, 'isTTY', { - value: isTTY, - enumerable: true, - writable: true, - configurable: true - }) -} - -Object.defineProperty(MuteStream.prototype, 'rows', { - get: function () { - return( this._dest ? this._dest.rows - : this._src ? this._src.rows - : undefined ) - }, enumerable: true, configurable: true }) - -Object.defineProperty(MuteStream.prototype, 'columns', { - get: function () { - return( this._dest ? this._dest.columns - : this._src ? this._src.columns - : undefined ) - }, enumerable: true, configurable: true }) - - -MuteStream.prototype.pipe = function (dest, options) { - this._dest = dest - return Stream.prototype.pipe.call(this, dest, options) -} - -MuteStream.prototype.pause = function () { - if (this._src) return this._src.pause() -} - -MuteStream.prototype.resume = function () { - if (this._src) return this._src.resume() -} - -MuteStream.prototype.write = function (c) { - if (this.muted) { - if (!this.replace) return true - if (c.match(/^\u001b/)) { - if(c.indexOf(this._prompt) === 0) { - c = c.substr(this._prompt.length); - c = c.replace(/./g, this.replace); - c = this._prompt + c; - } - this._hadControl = true - return this.emit('data', c) - } else { - if (this._prompt && this._hadControl && - c.indexOf(this._prompt) === 0) { - this._hadControl = false - this.emit('data', this._prompt) - c = c.substr(this._prompt.length) - } - c = c.toString().replace(/./g, this.replace) - } - } - this.emit('data', c) -} - -MuteStream.prototype.end = function (c) { - if (this.muted) { - if (c && this.replace) { - c = c.toString().replace(/./g, this.replace) - } else { - c = null - } - } - if (c) this.emit('data', c) - this.emit('end') -} - -function proxy (fn) { return function () { - var d = this._dest - var s = this._src - if (d && d[fn]) d[fn].apply(d, arguments) - if (s && s[fn]) s[fn].apply(s, arguments) -}} - -MuteStream.prototype.destroy = proxy('destroy') -MuteStream.prototype.destroySoon = proxy('destroySoon') -MuteStream.prototype.close = proxy('close') diff --git a/node_modules/mute-stream/package.json b/node_modules/mute-stream/package.json index 56ebb363b9251..37b2f5070ed69 100644 --- a/node_modules/mute-stream/package.json +++ b/node_modules/mute-stream/package.json @@ -1,29 +1,52 @@ { "name": "mute-stream", - "version": "0.0.8", - "main": "mute.js", - "directories": { - "test": "test" - }, + "version": "1.0.0", + "main": "lib/index.js", "devDependencies": { - "tap": "^12.1.1" + "@npmcli/eslint-config": "^4.0.0", + "@npmcli/template-oss": "4.11.0", + "tap": "^16.3.0" }, "scripts": { - "test": "tap test/*.js --cov" + "test": "tap", + "lint": "eslint \"**/*.js\"", + "postlint": "template-oss-check", + "template-oss-apply": "template-oss-apply --force", + "lintfix": "npm run lint -- --fix", + "snap": "tap", + "posttest": "npm run lint" }, "repository": { "type": "git", - "url": "git://github.com/isaacs/mute-stream" + "url": "https://github.com/npm/mute-stream.git" }, "keywords": [ "mute", "stream", "pipe" ], - "author": "Isaac Z. Schlueter (http://blog.izs.me/)", + "author": "GitHub Inc.", "license": "ISC", "description": "Bytes go in, but they don't come out (when muted).", "files": [ - "mute.js" - ] + "bin/", + "lib/" + ], + "tap": { + "statements": 70, + "branches": 60, + "functions": 81, + "lines": 70, + "nyc-arg": [ + "--exclude", + "tap-snapshots/**" + ] + }, + "engines": { + "node": "^14.17.0 || ^16.13.0 || >=18.0.0" + }, + "templateOSS": { + "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", + "version": "4.11.0" + } } diff --git a/node_modules/promzard/example/buffer.js b/node_modules/promzard/example/buffer.js deleted file mode 100644 index 828f9d1df9da2..0000000000000 --- a/node_modules/promzard/example/buffer.js +++ /dev/null @@ -1,12 +0,0 @@ -var pz = require('../promzard') - -var path = require('path') -var file = path.resolve(__dirname, 'substack-input.js') -var buf = require('fs').readFileSync(file) -var ctx = { basename: path.basename(path.dirname(file)) } - -pz.fromBuffer(buf, ctx, function (er, res) { - if (er) - throw er - console.error(JSON.stringify(res, null, 2)) -}) diff --git a/node_modules/promzard/example/index.js b/node_modules/promzard/example/index.js deleted file mode 100644 index 435131f3a6d1e..0000000000000 --- a/node_modules/promzard/example/index.js +++ /dev/null @@ -1,11 +0,0 @@ -var pz = require('../promzard') - -var path = require('path') -var file = path.resolve(__dirname, 'substack-input.js') -var ctx = { basename: path.basename(path.dirname(file)) } - -pz(file, ctx, function (er, res) { - if (er) - throw er - console.error(JSON.stringify(res, null, 2)) -}) diff --git a/node_modules/promzard/example/npm-init/init-input.js b/node_modules/promzard/example/npm-init/init-input.js deleted file mode 100644 index ba7781b3539e4..0000000000000 --- a/node_modules/promzard/example/npm-init/init-input.js +++ /dev/null @@ -1,191 +0,0 @@ -var fs = require('fs') -var path = require('path'); - -module.exports = { - "name" : prompt('name', - typeof name === 'undefined' - ? basename.replace(/^node-|[.-]js$/g, ''): name), - "version" : prompt('version', typeof version !== "undefined" - ? version : '0.0.0'), - "description" : (function () { - if (typeof description !== 'undefined' && description) { - return description - } - var value; - try { - var src = fs.readFileSync('README.md', 'utf8'); - value = src.split('\n').filter(function (line) { - return /\s+/.test(line) - && line.trim() !== basename.replace(/^node-/, '') - && !line.trim().match(/^#/) - ; - })[0] - .trim() - .replace(/^./, function (c) { return c.toLowerCase() }) - .replace(/\.$/, '') - ; - } - catch (e) { - try { - // Wouldn't it be nice if that file mattered? - var d = fs.readFileSync('.git/description', 'utf8') - } catch (e) {} - if (d.trim() && !value) value = d - } - return prompt('description', value); - })(), - "main" : (function () { - var f - try { - f = fs.readdirSync(dirname).filter(function (f) { - return f.match(/\.js$/) - }) - if (f.indexOf('index.js') !== -1) - f = 'index.js' - else if (f.indexOf('main.js') !== -1) - f = 'main.js' - else if (f.indexOf(basename + '.js') !== -1) - f = basename + '.js' - else - f = f[0] - } catch (e) {} - - return prompt('entry point', f || 'index.js') - })(), - "bin" : function (cb) { - fs.readdir(dirname + '/bin', function (er, d) { - // no bins - if (er) return cb() - // just take the first js file we find there, or nada - return cb(null, d.filter(function (f) { - return f.match(/\.js$/) - })[0]) - }) - }, - "directories" : function (cb) { - fs.readdir('.', function (er, dirs) { - if (er) return cb(er) - var res = {} - dirs.forEach(function (d) { - switch (d) { - case 'example': case 'examples': return res.example = d - case 'test': case 'tests': return res.test = d - case 'doc': case 'docs': return res.doc = d - case 'man': return res.man = d - } - }) - if (Object.keys(res).length === 0) res = undefined - return cb(null, res) - }) - }, - "dependencies" : typeof dependencies !== 'undefined' ? dependencies - : function (cb) { - fs.readdir('node_modules', function (er, dir) { - if (er) return cb() - var deps = {} - var n = dir.length - dir.forEach(function (d) { - if (d.match(/^\./)) return next() - if (d.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/)) - return next() - fs.readFile('node_modules/' + d + '/package.json', function (er, p) { - if (er) return next() - try { p = JSON.parse(p) } catch (e) { return next() } - if (!p.version) return next() - deps[d] = '~' + p.version - return next() - }) - }) - function next () { - if (--n === 0) return cb(null, deps) - } - }) - }, - "devDependencies" : typeof devDependencies !== 'undefined' ? devDependencies - : function (cb) { - // same as dependencies but for dev deps - fs.readdir('node_modules', function (er, dir) { - if (er) return cb() - var deps = {} - var n = dir.length - dir.forEach(function (d) { - if (d.match(/^\./)) return next() - if (!d.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/)) - return next() - fs.readFile('node_modules/' + d + '/package.json', function (er, p) { - if (er) return next() - try { p = JSON.parse(p) } catch (e) { return next() } - if (!p.version) return next() - deps[d] = '~' + p.version - return next() - }) - }) - function next () { - if (--n === 0) return cb(null, deps) - } - }) - }, - "scripts" : (function () { - // check to see what framework is in use, if any - try { var d = fs.readdirSync('node_modules') } - catch (e) { d = [] } - var s = typeof scripts === 'undefined' ? {} : scripts - - if (d.indexOf('coffee-script') !== -1) - s.prepublish = prompt('build command', - s.prepublish || 'coffee src/*.coffee -o lib') - - var notest = 'echo "Error: no test specified" && exit 1' - function tx (test) { - return test || notest - } - - if (!s.test || s.test === notest) { - if (d.indexOf('tap') !== -1) - s.test = prompt('test command', 'tap test/*.js', tx) - else if (d.indexOf('expresso') !== -1) - s.test = prompt('test command', 'expresso test', tx) - else if (d.indexOf('mocha') !== -1) - s.test = prompt('test command', 'mocha', tx) - else - s.test = prompt('test command', tx) - } - - return s - - })(), - - "repository" : (function () { - try { var gconf = fs.readFileSync('.git/config') } - catch (e) { gconf = null } - if (gconf) { - gconf = gconf.split(/\r?\n/) - var i = gconf.indexOf('[remote "origin"]') - if (i !== -1) { - var u = gconf[i + 1] - if (!u.match(/^\s*url =/)) u = gconf[i + 2] - if (!u.match(/^\s*url =/)) u = null - else u = u.replace(/^\s*url = /, '') - } - if (u && u.match(/^git@github.com:/)) - u = u.replace(/^git@github.com:/, 'git://github.com/') - } - - return prompt('git repository', u) - })(), - - "keywords" : prompt(function (s) { - if (!s) return undefined - if (Array.isArray(s)) s = s.join(' ') - if (typeof s !== 'string') return s - return s.split(/[\s,]+/) - }), - "author" : config['init.author.name'] - ? { - "name" : config['init.author.name'], - "email" : config['init.author.email'], - "url" : config['init.author.url'] - } - : undefined, - "license" : prompt('license', 'BSD') -} diff --git a/node_modules/promzard/example/npm-init/init.js b/node_modules/promzard/example/npm-init/init.js deleted file mode 100644 index 09484cd1c1991..0000000000000 --- a/node_modules/promzard/example/npm-init/init.js +++ /dev/null @@ -1,37 +0,0 @@ -var PZ = require('../../promzard').PromZard -var path = require('path') -var input = path.resolve(__dirname, 'init-input.js') - -var fs = require('fs') -var package = path.resolve(__dirname, 'package.json') -var pkg - -fs.readFile(package, 'utf8', function (er, d) { - if (er) ctx = {} - try { ctx = JSON.parse(d); pkg = JSON.parse(d) } - catch (e) { ctx = {} } - - ctx.dirname = path.dirname(package) - ctx.basename = path.basename(ctx.dirname) - if (!ctx.version) ctx.version = undefined - - // this should be replaced with the npm conf object - ctx.config = {} - - console.error('ctx=', ctx) - - var pz = new PZ(input, ctx) - - pz.on('data', function (data) { - console.error('pz data', data) - if (!pkg) pkg = {} - Object.keys(data).forEach(function (k) { - if (data[k] !== undefined && data[k] !== null) pkg[k] = data[k] - }) - console.error('package data %s', JSON.stringify(data, null, 2)) - fs.writeFile(package, JSON.stringify(pkg, null, 2), function (er) { - if (er) throw er - console.log('ok') - }) - }) -}) diff --git a/node_modules/promzard/example/npm-init/package.json b/node_modules/promzard/example/npm-init/package.json deleted file mode 100644 index 89c6d1fb6e2ac..0000000000000 --- a/node_modules/promzard/example/npm-init/package.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "name": "npm-init", - "version": "0.0.0", - "description": "an initter you init wit, innit?", - "main": "index.js", - "scripts": { - "test": "asdf" - }, - "license": "BSD" -} \ No newline at end of file diff --git a/node_modules/promzard/example/substack-input.js b/node_modules/promzard/example/substack-input.js deleted file mode 100644 index bf7aedb82d41f..0000000000000 --- a/node_modules/promzard/example/substack-input.js +++ /dev/null @@ -1,61 +0,0 @@ -module.exports = { - "name" : basename.replace(/^node-/, ''), - "version" : "0.0.0", - "description" : (function (cb) { - var fs = require('fs'); - var value; - try { - var src = fs.readFileSync('README.markdown', 'utf8'); - value = src.split('\n').filter(function (line) { - return /\s+/.test(line) - && line.trim() !== basename.replace(/^node-/, '') - ; - })[0] - .trim() - .replace(/^./, function (c) { return c.toLowerCase() }) - .replace(/\.$/, '') - ; - } - catch (e) {} - - return prompt('description', value); - })(), - "main" : prompt('entry point', 'index.js'), - "bin" : function (cb) { - var path = require('path'); - var fs = require('fs'); - var exists = fs.exists || path.exists; - exists('bin/cmd.js', function (ex) { - var bin - if (ex) { - var bin = {} - bin[basename.replace(/^node-/, '')] = 'bin/cmd.js' - } - cb(null, bin); - }); - }, - "directories" : { - "example" : "example", - "test" : "test" - }, - "dependencies" : {}, - "devDependencies" : { - "tap" : "~0.2.5" - }, - "scripts" : { - "test" : "tap test/*.js" - }, - "repository" : { - "type" : "git", - "url" : "git://github.com/substack/" + basename + ".git" - }, - "homepage" : "https://github.com/substack/" + basename, - "keywords" : prompt(function (s) { return s.split(/\s+/) }), - "author" : { - "name" : "James Halliday", - "email" : "mail@substack.net", - "url" : "http://substack.net" - }, - "license" : "MIT", - "engine" : { "node" : ">=0.6" } -} diff --git a/node_modules/promzard/lib/index.js b/node_modules/promzard/lib/index.js new file mode 100644 index 0000000000000..2244cbbbacdb0 --- /dev/null +++ b/node_modules/promzard/lib/index.js @@ -0,0 +1,175 @@ +const fs = require('fs/promises') +const { runInThisContext } = require('vm') +const { promisify } = require('util') +const { randomBytes } = require('crypto') +const { Module } = require('module') +const { dirname, basename } = require('path') +const read = require('read') + +const files = {} + +class PromZard { + #file = null + #backupFile = null + #ctx = null + #unique = randomBytes(8).toString('hex') + #prompts = [] + + constructor (file, ctx = {}, options = {}) { + this.#file = file + this.#ctx = ctx + this.#backupFile = options.backupFile + } + + static async promzard (file, ctx, options) { + const pz = new PromZard(file, ctx, options) + return pz.load() + } + + static async fromBuffer (buf, ctx, options) { + let filename = 0 + do { + filename = '\0' + Math.random() + } while (files[filename]) + files[filename] = buf + const ret = await PromZard.promzard(filename, ctx, options) + delete files[filename] + return ret + } + + async load () { + if (files[this.#file]) { + return this.#loaded() + } + + try { + files[this.#file] = await fs.readFile(this.#file, 'utf8') + } catch (er) { + if (er && this.#backupFile) { + this.#file = this.#backupFile + this.#backupFile = null + return this.load() + } + throw er + } + + return this.#loaded() + } + + async #loaded () { + const mod = new Module(this.#file, module) + mod.loaded = true + mod.filename = this.#file + mod.id = this.#file + mod.paths = Module._nodeModulePaths(dirname(this.#file)) + + this.#ctx.prompt = this.#makePrompt() + this.#ctx.__filename = this.#file + this.#ctx.__dirname = dirname(this.#file) + this.#ctx.__basename = basename(this.#file) + this.#ctx.module = mod + this.#ctx.require = (p) => mod.require(p) + this.#ctx.require.resolve = (p) => Module._resolveFilename(p, mod) + this.#ctx.exports = mod.exports + + const body = `(function(${Object.keys(this.#ctx).join(', ')}) { ${files[this.#file]}\n })` + runInThisContext(body, this.#file).apply(this.#ctx, Object.values(this.#ctx)) + this.#ctx.res = mod.exports + + return this.#walk() + } + + #makePrompt () { + return (...args) => { + let p, d, t + for (let i = 0; i < args.length; i++) { + const a = args[i] + if (typeof a === 'string') { + if (p) { + d = a + } else { + p = a + } + } else if (typeof a === 'function') { + t = a + } else if (a && typeof a === 'object') { + p = a.prompt || p + d = a.default || d + t = a.transform || t + } + } + try { + return `${this.#unique}-${this.#prompts.length}` + } finally { + this.#prompts.push([p, d, t]) + } + } + } + + async #walk (o = this.#ctx.res) { + const keys = Object.keys(o) + + const len = keys.length + let i = 0 + + while (i < len) { + const k = keys[i] + const v = o[k] + i++ + + if (v && typeof v === 'object') { + o[k] = await this.#walk(v) + continue + } + + if (v && typeof v === 'string' && v.startsWith(this.#unique)) { + const n = +v.slice(this.#unique.length + 1) + + // default to the key + // default to the ctx value, if there is one + const [prompt = k, def = this.#ctx[k], tx] = this.#prompts[n] + + try { + o[k] = await this.#prompt(prompt, def, tx) + } catch (er) { + if (er.notValid) { + console.log(er.message) + i-- + } else { + throw er + } + } + continue + } + + if (typeof v === 'function') { + // XXX: remove v.length check to remove cb from functions + // would be a breaking change for `npm init` + // XXX: if cb is no longer an argument then this.#ctx should + // be passed in to allow arrow fns to be used and still access ctx + const fn = v.length ? promisify(v) : v + o[k] = await fn.call(this.#ctx) + // back up so that we process this one again. + // this is because it might return a prompt() call in the cb. + i-- + continue + } + } + + return o + } + + async #prompt (prompt, def, tx) { + const res = await read({ prompt: prompt + ':', default: def }).then((r) => tx ? tx(r) : r) + // XXX: remove this to require throwing an error instead of + // returning it. would be a breaking change for `npm init` + if (res instanceof Error && res.notValid) { + throw res + } + return res + } +} + +module.exports = PromZard.promzard +module.exports.fromBuffer = PromZard.fromBuffer +module.exports.PromZard = PromZard diff --git a/node_modules/promzard/package.json b/node_modules/promzard/package.json index 77e3bd65513c9..a48764dd5441b 100644 --- a/node_modules/promzard/package.json +++ b/node_modules/promzard/package.json @@ -1,20 +1,48 @@ { - "author": "Isaac Z. Schlueter (http://blog.izs.me/)", + "author": "GitHub Inc.", "name": "promzard", "description": "prompting wizardly", - "version": "0.3.0", + "version": "1.0.0", "repository": { - "url": "git://github.com/isaacs/promzard" + "url": "https://github.com/npm/promzard.git", + "type": "git" }, "dependencies": { - "read": "1" + "read": "^2.0.0" }, "devDependencies": { - "tap": "~0.2.5" + "@npmcli/eslint-config": "^4.0.0", + "@npmcli/template-oss": "4.11.0", + "tap": "^16.3.0" }, - "main": "promzard.js", + "main": "lib/index.js", "scripts": { - "test": "tap test/*.js" + "test": "tap", + "lint": "eslint \"**/*.js\"", + "postlint": "template-oss-check", + "template-oss-apply": "template-oss-apply --force", + "lintfix": "npm run lint -- --fix", + "snap": "tap", + "posttest": "npm run lint" }, - "license": "ISC" + "license": "ISC", + "files": [ + "bin/", + "lib/" + ], + "engines": { + "node": "^14.17.0 || ^16.13.0 || >=18.0.0" + }, + "templateOSS": { + "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", + "version": "4.11.0" + }, + "tap": { + "jobs": 1, + "test-ignore": "fixtures/", + "nyc-arg": [ + "--exclude", + "tap-snapshots/**" + ] + } } diff --git a/node_modules/promzard/promzard.js b/node_modules/promzard/promzard.js deleted file mode 100644 index da1abca9535e4..0000000000000 --- a/node_modules/promzard/promzard.js +++ /dev/null @@ -1,238 +0,0 @@ -module.exports = promzard -promzard.PromZard = PromZard - -var fs = require('fs') -var vm = require('vm') -var util = require('util') -var files = {} -var crypto = require('crypto') -var EventEmitter = require('events').EventEmitter -var read = require('read') - -var Module = require('module').Module -var path = require('path') - -function promzard (file, ctx, cb) { - if (typeof ctx === 'function') cb = ctx, ctx = null; - if (!ctx) ctx = {}; - var pz = new PromZard(file, ctx) - pz.on('error', cb) - pz.on('data', function (data) { - cb(null, data) - }) -} -promzard.fromBuffer = function (buf, ctx, cb) { - var filename = 0 - do { - filename = '\0' + Math.random(); - } while (files[filename]) - files[filename] = buf - var ret = promzard(filename, ctx, cb) - delete files[filename] - return ret -} - -function PromZard (file, ctx) { - if (!(this instanceof PromZard)) - return new PromZard(file, ctx) - EventEmitter.call(this) - this.file = file - this.ctx = ctx - this.unique = crypto.randomBytes(8).toString('hex') - this.load() -} - -PromZard.prototype = Object.create( - EventEmitter.prototype, - { constructor: { - value: PromZard, - readable: true, - configurable: true, - writable: true, - enumerable: false } } ) - -PromZard.prototype.load = function () { - if (files[this.file]) - return this.loaded() - - fs.readFile(this.file, 'utf8', function (er, d) { - if (er && this.backupFile) { - this.file = this.backupFile - delete this.backupFile - return this.load() - } - if (er) - return this.emit('error', this.error = er) - files[this.file] = d - this.loaded() - }.bind(this)) -} - -PromZard.prototype.loaded = function () { - this.ctx.prompt = this.makePrompt() - this.ctx.__filename = this.file - this.ctx.__dirname = path.dirname(this.file) - this.ctx.__basename = path.basename(this.file) - var mod = this.ctx.module = this.makeModule() - this.ctx.require = function (path) { - return mod.require(path) - } - this.ctx.require.resolve = function(path) { - return Module._resolveFilename(path, mod); - } - this.ctx.exports = mod.exports - - this.script = this.wrap(files[this.file]) - var fn = vm.runInThisContext(this.script, this.file) - var args = Object.keys(this.ctx).map(function (k) { - return this.ctx[k] - }.bind(this)) - try { var res = fn.apply(this.ctx, args) } - catch (er) { this.emit('error', er) } - if (res && - typeof res === 'object' && - exports === mod.exports && - Object.keys(exports).length === 1) { - this.result = res - } else { - this.result = mod.exports - } - this.walk() -} - -PromZard.prototype.makeModule = function () { - var mod = new Module(this.file, module) - mod.loaded = true - mod.filename = this.file - mod.id = this.file - mod.paths = Module._nodeModulePaths(path.dirname(this.file)) - return mod -} - -PromZard.prototype.wrap = function (body) { - var s = '(function( %s ) { %s\n })' - var args = Object.keys(this.ctx).join(', ') - return util.format(s, args, body) -} - -PromZard.prototype.makePrompt = function () { - this.prompts = [] - return prompt.bind(this) - function prompt () { - var p, d, t - for (var i = 0; i < arguments.length; i++) { - var a = arguments[i] - if (typeof a === 'string' && p) - d = a - else if (typeof a === 'string') - p = a - else if (typeof a === 'function') - t = a - else if (a && typeof a === 'object') { - p = a.prompt || p - d = a.default || d - t = a.transform || t - } - } - - try { return this.unique + '-' + this.prompts.length } - finally { this.prompts.push([p, d, t]) } - } -} - -PromZard.prototype.walk = function (o, cb) { - o = o || this.result - cb = cb || function (er, res) { - if (er) - return this.emit('error', this.error = er) - this.result = res - return this.emit('data', res) - } - cb = cb.bind(this) - var keys = Object.keys(o) - var i = 0 - var len = keys.length - - L.call(this) - function L () { - if (this.error) - return - while (i < len) { - var k = keys[i] - var v = o[k] - i++ - - if (v && typeof v === 'object') { - return this.walk(v, function (er, res) { - if (er) return cb(er) - o[k] = res - L.call(this) - }.bind(this)) - } else if (v && - typeof v === 'string' && - v.indexOf(this.unique) === 0) { - var n = +v.substr(this.unique.length + 1) - var prompt = this.prompts[n] - if (isNaN(n) || !prompt) - continue - - // default to the key - if (undefined === prompt[0]) - prompt[0] = k - - // default to the ctx value, if there is one - if (undefined === prompt[1]) - prompt[1] = this.ctx[k] - - return this.prompt(prompt, function (er, res) { - if (er) { - if (!er.notValid) { - return this.emit('error', this.error = er); - } - console.log(er.message) - i -- - return L.call(this) - } - o[k] = res - L.call(this) - }.bind(this)) - } else if (typeof v === 'function') { - try { return v.call(this.ctx, function (er, res) { - if (er) - return this.emit('error', this.error = er) - o[k] = res - // back up so that we process this one again. - // this is because it might return a prompt() call in the cb. - i -- - L.call(this) - }.bind(this)) } - catch (er) { this.emit('error', er) } - } - } - // made it to the end of the loop, maybe - if (i >= len) - return cb(null, o) - } -} - -PromZard.prototype.prompt = function (pdt, cb) { - var prompt = pdt[0] - var def = pdt[1] - var tx = pdt[2] - - if (tx) { - cb = function (cb) { return function (er, data) { - try { - var res = tx(data) - if (!er && res instanceof Error && !!res.notValid) { - return cb(res, null) - } - return cb(er, res) - } - catch (er) { this.emit('error', er) } - }}(cb).bind(this) - } - - read({ prompt: prompt + ':' , default: def }, cb) -} - diff --git a/node_modules/promzard/test/basic.js b/node_modules/promzard/test/basic.js deleted file mode 100644 index ad1c92df9c4c0..0000000000000 --- a/node_modules/promzard/test/basic.js +++ /dev/null @@ -1,91 +0,0 @@ -var tap = require('tap') -var pz = require('../promzard.js') -var spawn = require('child_process').spawn - -tap.test('run the example', function (t) { - - var example = require.resolve('../example/index.js') - var node = process.execPath - - var expect = { - "name": "example", - "version": "0.0.0", - "description": "testing description", - "main": "test-entry.js", - "directories": { - "example": "example", - "test": "test" - }, - "dependencies": {}, - "devDependencies": { - "tap": "~0.2.5" - }, - "scripts": { - "test": "tap test/*.js" - }, - "repository": { - "type": "git", - "url": "git://github.com/substack/example.git" - }, - "homepage": "https://github.com/substack/example", - "keywords": [ - "fugazi", - "function", - "waiting", - "room" - ], - "author": { - "name": "James Halliday", - "email": "mail@substack.net", - "url": "http://substack.net" - }, - "license": "MIT", - "engine": { - "node": ">=0.6" - } - } - - console.error('%s %s', node, example) - var c = spawn(node, [example], { customFds: [-1,-1,-1] }) - var output = '' - c.stdout.on('data', function (d) { - output += d - respond() - }) - - var actual = '' - c.stderr.on('data', function (d) { - actual += d - }) - - function respond () { - console.error('respond', output) - if (output.match(/description: $/)) { - c.stdin.write('testing description\n') - return - } - if (output.match(/entry point: \(index\.js\) $/)) { - c.stdin.write('test-entry.js\n') - return - } - if (output.match(/keywords: $/)) { - c.stdin.write('fugazi function waiting room\n') - // "read" module is weird on node >= 0.10 when not a TTY - // requires explicit ending for reasons. - // could dig in, but really just wanna make tests pass, whatever. - c.stdin.end() - return - } - } - - c.on('exit', function () { - console.error('exit event') - }) - - c.on('close', function () { - console.error('actual', actual) - actual = JSON.parse(actual) - t.deepEqual(actual, expect) - t.end() - }) -}) diff --git a/node_modules/promzard/test/buffer.js b/node_modules/promzard/test/buffer.js deleted file mode 100644 index e1d240e2e4f48..0000000000000 --- a/node_modules/promzard/test/buffer.js +++ /dev/null @@ -1,84 +0,0 @@ -var tap = require('tap') -var pz = require('../promzard.js') -var spawn = require('child_process').spawn - -tap.test('run the example using a buffer', function (t) { - - var example = require.resolve('../example/buffer.js') - var node = process.execPath - - var expect = { - "name": "example", - "version": "0.0.0", - "description": "testing description", - "main": "test-entry.js", - "directories": { - "example": "example", - "test": "test" - }, - "dependencies": {}, - "devDependencies": { - "tap": "~0.2.5" - }, - "scripts": { - "test": "tap test/*.js" - }, - "repository": { - "type": "git", - "url": "git://github.com/substack/example.git" - }, - "homepage": "https://github.com/substack/example", - "keywords": [ - "fugazi", - "function", - "waiting", - "room" - ], - "author": { - "name": "James Halliday", - "email": "mail@substack.net", - "url": "http://substack.net" - }, - "license": "MIT", - "engine": { - "node": ">=0.6" - } - } - - var c = spawn(node, [example], { customFds: [-1,-1,-1] }) - var output = '' - c.stdout.on('data', function (d) { - output += d - respond() - }) - - var actual = '' - c.stderr.on('data', function (d) { - actual += d - }) - - function respond () { - if (output.match(/description: $/)) { - c.stdin.write('testing description\n') - return - } - if (output.match(/entry point: \(index\.js\) $/)) { - c.stdin.write('test-entry.js\n') - return - } - if (output.match(/keywords: $/)) { - c.stdin.write('fugazi function waiting room\n') - // "read" module is weird on node >= 0.10 when not a TTY - // requires explicit ending for reasons. - // could dig in, but really just wanna make tests pass, whatever. - c.stdin.end() - return - } - } - - c.on('close', function () { - actual = JSON.parse(actual) - t.deepEqual(actual, expect) - t.end() - }) -}) diff --git a/node_modules/promzard/test/exports.input b/node_modules/promzard/test/exports.input deleted file mode 100644 index 061cbfe1055aa..0000000000000 --- a/node_modules/promzard/test/exports.input +++ /dev/null @@ -1,5 +0,0 @@ -exports.a = 1 + 2 -exports.b = prompt('To be or not to be?', '!2b') -exports.c = {} -exports.c.x = prompt() -exports.c.y = tmpdir + "/y/file.txt" diff --git a/node_modules/promzard/test/exports.js b/node_modules/promzard/test/exports.js deleted file mode 100644 index c17993a4e9e75..0000000000000 --- a/node_modules/promzard/test/exports.js +++ /dev/null @@ -1,48 +0,0 @@ -var test = require('tap').test; -var promzard = require('../'); - -if (process.argv[2] === 'child') { - return child() -} - -test('exports', function (t) { - t.plan(1); - - var spawn = require('child_process').spawn - var child = spawn(process.execPath, [__filename, 'child']) - - var output = '' - child.stderr.on('data', function (c) { - output += c - }) - - setTimeout(function () { - child.stdin.write('\n'); - }, 100) - setTimeout(function () { - child.stdin.end('55\n'); - }, 200) - - child.on('close', function () { - console.error('output=%j', output) - output = JSON.parse(output) - t.same({ - a : 3, - b : '!2b', - c : { - x : 55, - y : '/tmp/y/file.txt', - } - }, output); - t.end() - }) -}); - -function child () { - var ctx = { tmpdir : '/tmp' } - var file = __dirname + '/exports.input'; - - promzard(file, ctx, function (err, output) { - console.error(JSON.stringify(output)) - }); -} diff --git a/node_modules/promzard/test/fn.input b/node_modules/promzard/test/fn.input deleted file mode 100644 index ed6c3f1c80c5b..0000000000000 --- a/node_modules/promzard/test/fn.input +++ /dev/null @@ -1,18 +0,0 @@ -var fs = require('fs') - -module.exports = { - "a": 1 + 2, - "b": prompt('To be or not to be?', '!2b', function (s) { - return s.toUpperCase() + '...' - }), - "c": { - "x": prompt(function (x) { return x * 100 }), - "y": tmpdir + "/y/file.txt" - }, - a_function: function (cb) { - fs.readFile(__filename, 'utf8', cb) - }, - asyncPrompt: function (cb) { - return cb(null, prompt('a prompt at any other time would smell as sweet')) - } -} diff --git a/node_modules/promzard/test/fn.js b/node_modules/promzard/test/fn.js deleted file mode 100644 index 899ebedbdd010..0000000000000 --- a/node_modules/promzard/test/fn.js +++ /dev/null @@ -1,56 +0,0 @@ -var test = require('tap').test; -var promzard = require('../'); -var fs = require('fs') -var file = __dirname + '/fn.input'; - -var expect = { - a : 3, - b : '!2B...', - c : { - x : 5500, - y : '/tmp/y/file.txt', - } -} -expect.a_function = fs.readFileSync(file, 'utf8') -expect.asyncPrompt = 'async prompt' - -if (process.argv[2] === 'child') { - return child() -} - -test('prompt callback param', function (t) { - t.plan(1); - - var spawn = require('child_process').spawn - var child = spawn(process.execPath, [__filename, 'child']) - - var output = '' - child.stderr.on('data', function (c) { - output += c - }) - - child.on('close', function () { - console.error('output=%j', output) - output = JSON.parse(output) - t.same(output, expect); - t.end() - }) - - setTimeout(function () { - child.stdin.write('\n') - }, 100) - setTimeout(function () { - child.stdin.write('55\n') - }, 150) - setTimeout(function () { - child.stdin.end('async prompt\n') - }, 200) -}) - -function child () { - var ctx = { tmpdir : '/tmp' } - var file = __dirname + '/fn.input'; - promzard(file, ctx, function (err, output) { - console.error(JSON.stringify(output)) - }) -} diff --git a/node_modules/promzard/test/simple.input b/node_modules/promzard/test/simple.input deleted file mode 100644 index e49def6470d59..0000000000000 --- a/node_modules/promzard/test/simple.input +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - "a": 1 + 2, - "b": prompt('To be or not to be?', '!2b'), - "c": { - "x": prompt(), - "y": tmpdir + "/y/file.txt" - } -} diff --git a/node_modules/promzard/test/simple.js b/node_modules/promzard/test/simple.js deleted file mode 100644 index 034a86475afbd..0000000000000 --- a/node_modules/promzard/test/simple.js +++ /dev/null @@ -1,30 +0,0 @@ -var test = require('tap').test; -var promzard = require('../'); - -test('simple', function (t) { - t.plan(1); - - var ctx = { tmpdir : '/tmp' } - var file = __dirname + '/simple.input'; - promzard(file, ctx, function (err, output) { - t.same( - { - a : 3, - b : '!2b', - c : { - x : 55, - y : '/tmp/y/file.txt', - } - }, - output - ); - }); - - setTimeout(function () { - process.stdin.emit('data', '\n'); - }, 100); - - setTimeout(function () { - process.stdin.emit('data', '55\n'); - }, 200); -}); diff --git a/node_modules/promzard/test/validate.input b/node_modules/promzard/test/validate.input deleted file mode 100644 index 839c0652294ac..0000000000000 --- a/node_modules/promzard/test/validate.input +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - "name": prompt("name", function (data) { - if (data === 'cool') return data - var er = new Error('not cool') - er.notValid = true - return er - }) -} diff --git a/node_modules/promzard/test/validate.js b/node_modules/promzard/test/validate.js deleted file mode 100644 index a120681494e60..0000000000000 --- a/node_modules/promzard/test/validate.js +++ /dev/null @@ -1,20 +0,0 @@ - -var promzard = require('../') -var test = require('tap').test - -test('validate', function (t) { - t.plan(2) - var ctx = { tmpdir : '/tmp' } - var file = __dirname + '/validate.input' - promzard(file, ctx, function (er, found) { - t.ok(!er) - var wanted = { name: 'cool' } - t.same(found, wanted) - }) - setTimeout(function () { - process.stdin.emit('data', 'not cool\n') - }, 100) - setTimeout(function () { - process.stdin.emit('data', 'cool\n') - }, 200) -}) diff --git a/node_modules/read/lib/read.js b/node_modules/read/lib/read.js index a93d1b3b532c0..882b11c285bcf 100644 --- a/node_modules/read/lib/read.js +++ b/node_modules/read/lib/read.js @@ -1,113 +1,82 @@ - -module.exports = read - -var readline = require('readline') -var Mute = require('mute-stream') - -function read (opts, cb) { - if (opts.num) { - throw new Error('read() no longer accepts a char number limit') - } - - if (typeof opts.default !== 'undefined' && - typeof opts.default !== 'string' && - typeof opts.default !== 'number') { +const readline = require('readline') +const Mute = require('mute-stream') + +module.exports = async function read ({ + default: def = '', + input = process.stdin, + output = process.stdout, + prompt = '', + silent, + timeout, + edit, + terminal, + replace, +}) { + if (typeof def !== 'undefined' && typeof def !== 'string' && typeof def !== 'number') { throw new Error('default value must be string or number') } - var input = opts.input || process.stdin - var output = opts.output || process.stdout - var prompt = (opts.prompt || '').trim() + ' ' - var silent = opts.silent - var editDef = false - var timeout = opts.timeout + let editDef = false + prompt = prompt.trim() + ' ' + terminal = !!(terminal || output.isTTY) - var def = opts.default || '' if (def) { if (silent) { prompt += '(