Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib/node_mod.../npm/lib
File: version.js
'use strict'
[0] Fix | Delete
const BB = require('bluebird')
[1] Fix | Delete
[2] Fix | Delete
const assert = require('assert')
[3] Fix | Delete
const chain = require('slide').chain
[4] Fix | Delete
const detectIndent = require('detect-indent')
[5] Fix | Delete
const detectNewline = require('detect-newline')
[6] Fix | Delete
const fs = require('graceful-fs')
[7] Fix | Delete
const readFile = BB.promisify(require('graceful-fs').readFile)
[8] Fix | Delete
const git = require('./utils/git.js')
[9] Fix | Delete
const lifecycle = require('./utils/lifecycle.js')
[10] Fix | Delete
const log = require('npmlog')
[11] Fix | Delete
const npm = require('./npm.js')
[12] Fix | Delete
const output = require('./utils/output.js')
[13] Fix | Delete
const parseJSON = require('./utils/parse-json.js')
[14] Fix | Delete
const path = require('path')
[15] Fix | Delete
const semver = require('semver')
[16] Fix | Delete
const stringifyPackage = require('stringify-package')
[17] Fix | Delete
const writeFileAtomic = require('write-file-atomic')
[18] Fix | Delete
[19] Fix | Delete
version.usage = 'npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]' +
[20] Fix | Delete
'\n(run in package dir)\n' +
[21] Fix | Delete
"'npm -v' or 'npm --version' to print npm version " +
[22] Fix | Delete
'(' + npm.version + ')\n' +
[23] Fix | Delete
"'npm view <pkg> version' to view a package's " +
[24] Fix | Delete
'published version\n' +
[25] Fix | Delete
"'npm ls' to inspect current package/dependency versions"
[26] Fix | Delete
[27] Fix | Delete
// npm version <newver>
[28] Fix | Delete
module.exports = version
[29] Fix | Delete
function version (args, silent, cb_) {
[30] Fix | Delete
if (typeof cb_ !== 'function') {
[31] Fix | Delete
cb_ = silent
[32] Fix | Delete
silent = false
[33] Fix | Delete
}
[34] Fix | Delete
if (args.length > 1) return cb_(version.usage)
[35] Fix | Delete
[36] Fix | Delete
readPackage(function (er, data, indent, newline) {
[37] Fix | Delete
if (!args.length) return dump(data, cb_)
[38] Fix | Delete
[39] Fix | Delete
if (er) {
[40] Fix | Delete
log.error('version', 'No valid package.json found')
[41] Fix | Delete
return cb_(er)
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
if (args[0] === 'from-git') {
[45] Fix | Delete
retrieveTagVersion(silent, data, cb_)
[46] Fix | Delete
} else {
[47] Fix | Delete
var newVersion = semver.valid(args[0])
[48] Fix | Delete
if (!newVersion) newVersion = semver.inc(data.version, args[0], npm.config.get('preid'))
[49] Fix | Delete
if (!newVersion) return cb_(version.usage)
[50] Fix | Delete
persistVersion(newVersion, silent, data, cb_)
[51] Fix | Delete
}
[52] Fix | Delete
})
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
function retrieveTagVersion (silent, data, cb_) {
[56] Fix | Delete
chain([
[57] Fix | Delete
verifyGit,
[58] Fix | Delete
parseLastGitTag
[59] Fix | Delete
], function (er, results) {
[60] Fix | Delete
if (er) return cb_(er)
[61] Fix | Delete
var localData = {
[62] Fix | Delete
hasGit: true,
[63] Fix | Delete
existingTag: true
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
var version = results[results.length - 1]
[67] Fix | Delete
persistVersion(version, silent, data, localData, cb_)
[68] Fix | Delete
})
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
function parseLastGitTag (cb) {
[72] Fix | Delete
var options = { env: process.env }
[73] Fix | Delete
git.whichAndExec(['describe', '--abbrev=0'], options, function (er, stdout) {
[74] Fix | Delete
if (er) {
[75] Fix | Delete
if (er.message.indexOf('No names found') !== -1) return cb(new Error('No tags found'))
[76] Fix | Delete
return cb(er)
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
var tag = stdout.trim()
[80] Fix | Delete
var prefix = npm.config.get('tag-version-prefix')
[81] Fix | Delete
// Strip the prefix from the start of the tag:
[82] Fix | Delete
if (tag.indexOf(prefix) === 0) tag = tag.slice(prefix.length)
[83] Fix | Delete
var version = semver.valid(tag)
[84] Fix | Delete
if (!version) return cb(new Error(tag + ' is not a valid version'))
[85] Fix | Delete
cb(null, version)
[86] Fix | Delete
})
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
function persistVersion (newVersion, silent, data, localData, cb_) {
[90] Fix | Delete
if (typeof localData === 'function') {
[91] Fix | Delete
cb_ = localData
[92] Fix | Delete
localData = {}
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
if (!npm.config.get('allow-same-version') && data.version === newVersion) {
[96] Fix | Delete
return cb_(new Error('Version not changed, might want --allow-same-version'))
[97] Fix | Delete
}
[98] Fix | Delete
data.version = newVersion
[99] Fix | Delete
var lifecycleData = Object.create(data)
[100] Fix | Delete
lifecycleData._id = data.name + '@' + newVersion
[101] Fix | Delete
[102] Fix | Delete
var where = npm.prefix
[103] Fix | Delete
chain([
[104] Fix | Delete
!localData.hasGit && [checkGit, localData],
[105] Fix | Delete
[lifecycle, lifecycleData, 'preversion', where],
[106] Fix | Delete
[updatePackage, newVersion, silent],
[107] Fix | Delete
[lifecycle, lifecycleData, 'version', where],
[108] Fix | Delete
[commit, localData, newVersion],
[109] Fix | Delete
[lifecycle, lifecycleData, 'postversion', where]
[110] Fix | Delete
], cb_)
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
function readPackage (cb) {
[114] Fix | Delete
var packagePath = path.join(npm.localPrefix, 'package.json')
[115] Fix | Delete
fs.readFile(packagePath, 'utf8', function (er, data) {
[116] Fix | Delete
if (er) return cb(new Error(er))
[117] Fix | Delete
var indent
[118] Fix | Delete
var newline
[119] Fix | Delete
try {
[120] Fix | Delete
indent = detectIndent(data).indent
[121] Fix | Delete
newline = detectNewline(data)
[122] Fix | Delete
data = JSON.parse(data)
[123] Fix | Delete
} catch (e) {
[124] Fix | Delete
er = e
[125] Fix | Delete
data = null
[126] Fix | Delete
}
[127] Fix | Delete
cb(er, data, indent, newline)
[128] Fix | Delete
})
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
function updatePackage (newVersion, silent, cb_) {
[132] Fix | Delete
function cb (er) {
[133] Fix | Delete
if (!er && !silent) output('v' + newVersion)
[134] Fix | Delete
cb_(er)
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
readPackage(function (er, data, indent, newline) {
[138] Fix | Delete
if (er) return cb(new Error(er))
[139] Fix | Delete
data.version = newVersion
[140] Fix | Delete
write(data, 'package.json', indent, newline, cb)
[141] Fix | Delete
})
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
function commit (localData, newVersion, cb) {
[145] Fix | Delete
updateShrinkwrap(newVersion, function (er, hasShrinkwrap, hasLock) {
[146] Fix | Delete
if (er || !localData.hasGit) return cb(er)
[147] Fix | Delete
localData.hasShrinkwrap = hasShrinkwrap
[148] Fix | Delete
localData.hasPackageLock = hasLock
[149] Fix | Delete
_commit(newVersion, localData, cb)
[150] Fix | Delete
})
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
const SHRINKWRAP = 'npm-shrinkwrap.json'
[154] Fix | Delete
const PKGLOCK = 'package-lock.json'
[155] Fix | Delete
[156] Fix | Delete
function readLockfile (name) {
[157] Fix | Delete
return readFile(
[158] Fix | Delete
path.join(npm.localPrefix, name), 'utf8'
[159] Fix | Delete
).catch({code: 'ENOENT'}, () => null)
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
function updateShrinkwrap (newVersion, cb) {
[163] Fix | Delete
BB.join(
[164] Fix | Delete
readLockfile(SHRINKWRAP),
[165] Fix | Delete
readLockfile(PKGLOCK),
[166] Fix | Delete
(shrinkwrap, lockfile) => {
[167] Fix | Delete
if (!shrinkwrap && !lockfile) {
[168] Fix | Delete
return cb(null, false, false)
[169] Fix | Delete
}
[170] Fix | Delete
const file = shrinkwrap ? SHRINKWRAP : PKGLOCK
[171] Fix | Delete
let data
[172] Fix | Delete
let indent
[173] Fix | Delete
let newline
[174] Fix | Delete
try {
[175] Fix | Delete
data = parseJSON(shrinkwrap || lockfile)
[176] Fix | Delete
indent = detectIndent(shrinkwrap || lockfile).indent
[177] Fix | Delete
newline = detectNewline(shrinkwrap || lockfile)
[178] Fix | Delete
} catch (err) {
[179] Fix | Delete
log.error('version', `Bad ${file} data.`)
[180] Fix | Delete
return cb(err)
[181] Fix | Delete
}
[182] Fix | Delete
data.version = newVersion
[183] Fix | Delete
write(data, file, indent, newline, (err) => {
[184] Fix | Delete
if (err) {
[185] Fix | Delete
log.error('version', `Failed to update version in ${file}`)
[186] Fix | Delete
return cb(err)
[187] Fix | Delete
} else {
[188] Fix | Delete
return cb(null, !!shrinkwrap, !!lockfile)
[189] Fix | Delete
}
[190] Fix | Delete
})
[191] Fix | Delete
}
[192] Fix | Delete
)
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
function dump (data, cb) {
[196] Fix | Delete
var v = {}
[197] Fix | Delete
[198] Fix | Delete
if (data && data.name && data.version) v[data.name] = data.version
[199] Fix | Delete
v.npm = npm.version
[200] Fix | Delete
Object.keys(process.versions).sort().forEach(function (k) {
[201] Fix | Delete
v[k] = process.versions[k]
[202] Fix | Delete
})
[203] Fix | Delete
[204] Fix | Delete
if (npm.config.get('json')) v = JSON.stringify(v, null, 2)
[205] Fix | Delete
[206] Fix | Delete
output(v)
[207] Fix | Delete
cb()
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
function statGitFolder (cb) {
[211] Fix | Delete
fs.stat(path.join(npm.localPrefix, '.git'), cb)
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
function callGitStatus (cb) {
[215] Fix | Delete
git.whichAndExec(
[216] Fix | Delete
[ 'status', '--porcelain' ],
[217] Fix | Delete
{ env: process.env },
[218] Fix | Delete
cb
[219] Fix | Delete
)
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
function cleanStatusLines (stdout) {
[223] Fix | Delete
var lines = stdout.trim().split('\n').filter(function (line) {
[224] Fix | Delete
return line.trim() && !line.match(/^\?\? /)
[225] Fix | Delete
}).map(function (line) {
[226] Fix | Delete
return line.trim()
[227] Fix | Delete
})
[228] Fix | Delete
[229] Fix | Delete
return lines
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
function verifyGit (cb) {
[233] Fix | Delete
function checkStatus (er) {
[234] Fix | Delete
if (er) return cb(er)
[235] Fix | Delete
callGitStatus(checkStdout)
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
function checkStdout (er, stdout) {
[239] Fix | Delete
if (er) return cb(er)
[240] Fix | Delete
var lines = cleanStatusLines(stdout)
[241] Fix | Delete
if (lines.length > 0) {
[242] Fix | Delete
return cb(new Error(
[243] Fix | Delete
'Git working directory not clean.\n' + lines.join('\n')
[244] Fix | Delete
))
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
cb()
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
statGitFolder(checkStatus)
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
function checkGit (localData, cb) {
[254] Fix | Delete
statGitFolder(function (er) {
[255] Fix | Delete
var doGit = !er && npm.config.get('git-tag-version')
[256] Fix | Delete
if (!doGit) {
[257] Fix | Delete
if (er && npm.config.get('git-tag-version')) log.verbose('version', 'error checking for .git', er)
[258] Fix | Delete
log.verbose('version', 'not tagging in git')
[259] Fix | Delete
return cb(null, false)
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
// check for git
[263] Fix | Delete
callGitStatus(function (er, stdout) {
[264] Fix | Delete
if (er && er.code === 'ENOGIT') {
[265] Fix | Delete
log.warn(
[266] Fix | Delete
'version',
[267] Fix | Delete
'This is a Git checkout, but the git command was not found.',
[268] Fix | Delete
'npm could not create a Git tag for this release!'
[269] Fix | Delete
)
[270] Fix | Delete
return cb(null, false)
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
var lines = cleanStatusLines(stdout)
[274] Fix | Delete
if (lines.length && !npm.config.get('force')) {
[275] Fix | Delete
return cb(new Error(
[276] Fix | Delete
'Git working directory not clean.\n' + lines.join('\n')
[277] Fix | Delete
))
[278] Fix | Delete
}
[279] Fix | Delete
localData.hasGit = true
[280] Fix | Delete
cb(null, true)
[281] Fix | Delete
})
[282] Fix | Delete
})
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
module.exports.buildCommitArgs = buildCommitArgs
[286] Fix | Delete
function buildCommitArgs (args) {
[287] Fix | Delete
const add = []
[288] Fix | Delete
args = args || []
[289] Fix | Delete
if (args[0] === 'commit') args.shift()
[290] Fix | Delete
if (!npm.config.get('commit-hooks')) add.push('-n')
[291] Fix | Delete
if (npm.config.get('allow-same-version')) add.push('--allow-empty')
[292] Fix | Delete
return ['commit', ...add, ...args]
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
module.exports.buildTagFlags = buildTagFlags
[296] Fix | Delete
function buildTagFlags () {
[297] Fix | Delete
return '-'.concat(
[298] Fix | Delete
npm.config.get('sign-git-tag') ? 's' : '',
[299] Fix | Delete
npm.config.get('allow-same-version') ? 'f' : '',
[300] Fix | Delete
'm'
[301] Fix | Delete
)
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
function _commit (version, localData, cb) {
[305] Fix | Delete
const options = { env: process.env }
[306] Fix | Delete
const message = npm.config.get('message').replace(/%s/g, version)
[307] Fix | Delete
const signCommit = npm.config.get('sign-git-commit')
[308] Fix | Delete
const commitArgs = buildCommitArgs([
[309] Fix | Delete
'commit',
[310] Fix | Delete
...(signCommit ? ['-S', '-m'] : ['-m']),
[311] Fix | Delete
message
[312] Fix | Delete
])
[313] Fix | Delete
[314] Fix | Delete
stagePackageFiles(localData, options).then(() => {
[315] Fix | Delete
return git.exec(commitArgs, options)
[316] Fix | Delete
}).then(() => {
[317] Fix | Delete
if (!localData.existingTag) {
[318] Fix | Delete
return git.exec([
[319] Fix | Delete
'tag', npm.config.get('tag-version-prefix') + version,
[320] Fix | Delete
buildTagFlags(), message
[321] Fix | Delete
], options)
[322] Fix | Delete
}
[323] Fix | Delete
}).nodeify(cb)
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
function stagePackageFiles (localData, options) {
[327] Fix | Delete
return addLocalFile('package.json', options, false).then(() => {
[328] Fix | Delete
if (localData.hasShrinkwrap) {
[329] Fix | Delete
return addLocalFile('npm-shrinkwrap.json', options, true)
[330] Fix | Delete
} else if (localData.hasPackageLock) {
[331] Fix | Delete
return addLocalFile('package-lock.json', options, true)
[332] Fix | Delete
}
[333] Fix | Delete
})
[334] Fix | Delete
}
[335] Fix | Delete
[336] Fix | Delete
function addLocalFile (file, options, ignoreFailure) {
[337] Fix | Delete
const p = git.exec(['add', path.join(npm.localPrefix, file)], options)
[338] Fix | Delete
return ignoreFailure
[339] Fix | Delete
? p.catch(() => {})
[340] Fix | Delete
: p
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
function write (data, file, indent, newline, cb) {
[344] Fix | Delete
assert(data && typeof data === 'object', 'must pass data to version write')
[345] Fix | Delete
assert(typeof file === 'string', 'must pass filename to write to version write')
[346] Fix | Delete
[347] Fix | Delete
log.verbose('version.write', 'data', data, 'to', file)
[348] Fix | Delete
writeFileAtomic(
[349] Fix | Delete
path.join(npm.localPrefix, file),
[350] Fix | Delete
stringifyPackage(data, indent, newline),
[351] Fix | Delete
cb
[352] Fix | Delete
)
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function