-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.0.0 rm reset cmd + prefer snake_case
- Loading branch information
Showing
6 changed files
with
70 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,142 +1,144 @@ | ||
const fs = require('fs'); | ||
const { promisify } = require('util'); | ||
const path = require('path'); | ||
const execAsync = require('./util/exec-async'); | ||
const exec_async = require('./util/exec-async'); | ||
|
||
const storage = '~/.upgrade-deps'; | ||
|
||
const writeFileAsync = promisify( fs.writeFile ); | ||
const semverRegexStr = '[0-9]+\\.[0-9]+\\.[0-9]+'; | ||
const semverRegex = new RegExp( semverRegexStr ); | ||
const gitSemverRegex = new RegExp( `^.+#${ semverRegexStr }$` ); | ||
const write_file_async = promisify( fs.writeFile ); | ||
const semver_regex_str = '[0-9]+\\.[0-9]+\\.[0-9]+'; | ||
const semver_regex = new RegExp( semver_regex_str ); | ||
const git_semver_regex = new RegExp( `^.+#${ semver_regex_str }$` ); | ||
|
||
const getLatestNpm = async( pkgName ) => { | ||
const { stdout, stderr } = await execAsync( `npm show ${ pkgName } version` ); | ||
async function get_latest_npm( pkg_name ) { | ||
const { stdout, stderr } = await exec_async( `npm show ${ pkg_name } version` ); | ||
|
||
if ( stderr ) { | ||
throw new Error( stderr ); | ||
} | ||
|
||
return stdout.trim(); | ||
}; | ||
} | ||
|
||
const getLatestGit = async( version ) => { | ||
async function get_latest_git( version ) { | ||
const [ repo ] = version.split('#'); | ||
|
||
const destination = `${ storage }/${ repo }`; | ||
|
||
await execAsync( `git clone [email protected]:${ repo } ${ destination }` ); | ||
await exec_async( `git clone [email protected]:${ repo } ${ destination }` ); | ||
|
||
const { stdout: latestTag } = await execAsync([ | ||
const { stdout: latest_tag } = await exec_async([ | ||
`cd ${ destination }`, | ||
'&& git describe --tags `git rev-list --tags --max-count=1`' | ||
].join(' ') ); | ||
|
||
return `${ repo }#${ latestTag.trim() }`; | ||
}; | ||
return `${ repo }#${ latest_tag.trim() }`; | ||
} | ||
|
||
const getLatest = async([ pkgName, version ]) => { | ||
async function get_latest([ pkg_name, version ]) { | ||
let latest = version; | ||
|
||
if ( gitSemverRegex.test( version ) ) { | ||
latest = await getLatestGit( version ); | ||
} else if ( semverRegex.test( version ) ) { | ||
latest = await getLatestNpm( pkgName ); | ||
if ( git_semver_regex.test( version ) ) { | ||
latest = await get_latest_git( version ); | ||
} else if ( semver_regex.test( version ) ) { | ||
latest = await get_latest_npm( pkg_name ); | ||
} | ||
|
||
return [ pkgName, latest ]; | ||
}; | ||
return [ pkg_name, latest ]; | ||
} | ||
|
||
const getPackageJSON = () => { | ||
const pkgJSONPath = path.join( process.cwd(), 'package.json' ); | ||
function get_pkg_json() { | ||
const pkg_json_path = path.join( process.cwd(), 'package.json' ); | ||
|
||
try { | ||
return require( pkgJSONPath ); | ||
return require( pkg_json_path ); | ||
} catch ( ex ) { | ||
if ( ex.code === 'MODULE_NOT_FOUND' ) { | ||
throw new Error( `couldnt find package.json in current directory: ${ pkgJSONPath }` ); | ||
throw new Error( `couldnt find package.json in current directory: ${ pkg_json_path }` ); | ||
} | ||
throw ex; | ||
} | ||
}; | ||
} | ||
|
||
const upgradeDeps = async({ breaking, dryRun }) => { | ||
async function upgrade_deps({ breaking, dryRun: dry_run }) { | ||
try { | ||
const packageJSON = getPackageJSON(); | ||
const pkg_json = get_pkg_json(); | ||
|
||
await execAsync( `[ ! -d ${ storage } ]` ).catch( () => { | ||
await exec_async( `[ ! -d ${ storage } ]` ).catch( () => { | ||
console.log( `State directory "${ storage }" must be deleted to continue` ); | ||
process.exit( 1 ); | ||
}); | ||
|
||
await execAsync( `mkdir -p ${ storage }` ); | ||
await exec_async( `mkdir -p ${ storage }` ); | ||
|
||
const depsPromise = Promise.all( | ||
Object.entries( packageJSON.dependencies || {} ).map( getLatest ) | ||
const deps_promise = Promise.all( | ||
Object.entries( pkg_json.dependencies || {} ).map( get_latest ) | ||
); | ||
|
||
const devDepsPromise = Promise.all( | ||
Object.entries( packageJSON.devDependencies || {} ).map( getLatest ) | ||
const dev_deps_promise = Promise.all( | ||
Object.entries( pkg_json.devDependencies || {} ).map( get_latest ) | ||
); | ||
|
||
|
||
const [ dependencies, devDependencies ] = await Promise.all([ | ||
depsPromise, | ||
devDepsPromise | ||
const [ dependencies, dev_dependencies ] = await Promise.all([ | ||
deps_promise, | ||
dev_deps_promise | ||
]); | ||
|
||
const updated = Object.assign( {}, packageJSON ); | ||
const dryRunUpdates = { dependencies: {}, devDependencies: {} }; | ||
const updated = Object.assign( {}, pkg_json ); | ||
const dry_run_updates = { dependencies: {}, devDependencies: {} }; | ||
|
||
if ( dependencies.length ) { | ||
updated.dependencies = Object.fromEntries( | ||
dependencies.map( ([name, version]) => { | ||
const prevVersion = packageJSON.dependencies[name]; | ||
const majorBump = prevVersion.split('.')[0] !== version.split('.')[0]; | ||
const newVersion = !majorBump || breaking ? version : prevVersion; | ||
const prev_version = pkg_json.dependencies[name]; | ||
const major_bump = prev_version.split('.')[0] !== version.split('.')[0]; | ||
const new_version = !major_bump || breaking ? version : prev_version; | ||
|
||
if ( prevVersion !== newVersion ) { | ||
dryRunUpdates.dependencies[ name ] = `${ prevVersion } -> ${ newVersion }`; | ||
if ( prev_version !== new_version ) { | ||
dry_run_updates.dependencies[ name ] = `${ prev_version } -> ${ new_version }`; | ||
} | ||
|
||
return [name, newVersion]; | ||
return [name, new_version]; | ||
}) | ||
); | ||
} | ||
|
||
if ( devDependencies.length ) { | ||
if ( dev_dependencies.length ) { | ||
updated.devDependencies = Object.fromEntries( | ||
devDependencies.map( ([name, version]) => { | ||
const prevVersion = packageJSON.devDependencies[name]; | ||
const majorBump = prevVersion.split('.')[0] !== version.split('.')[0]; | ||
const newVersion = !majorBump || breaking ? version : prevVersion; | ||
dev_dependencies.map( ([name, version]) => { | ||
const prev_version = pkg_json.devDependencies[name]; | ||
const major_bump = prev_version.split('.')[0] !== version.split('.')[0]; | ||
const new_version = !major_bump || breaking ? version : prev_version; | ||
|
||
if ( prevVersion !== newVersion ) { | ||
dryRunUpdates.devDependencies[ name ] = `${ prevVersion } -> ${ newVersion }`; | ||
if ( prev_version !== new_version ) { | ||
dry_run_updates.devDependencies[ name ] = `${ prev_version } -> ${ new_version }`; | ||
} | ||
|
||
return [name, newVersion]; | ||
return [name, new_version]; | ||
}) | ||
); | ||
} | ||
|
||
if ( dryRun ) { | ||
if ( dry_run ) { | ||
console.log('Dry run package updates:\n'); | ||
console.log( JSON.stringify( dryRunUpdates, null, 2 ) ); | ||
console.log( JSON.stringify( dry_run_updates, null, 2 ) ); | ||
} else { | ||
await writeFileAsync( 'package.json', JSON.stringify( updated, null, 2 ).trim() ); | ||
await write_file_async( | ||
'package.json', | ||
JSON.stringify( updated, null, 2 ).trim() + '\n' | ||
); | ||
} | ||
|
||
await execAsync( `rm -rf ${ storage }` ); | ||
await exec_async( `rm -rf ${ storage }` ); | ||
|
||
process.exit( 0 ); | ||
} catch ( ex ) { | ||
console.error( 'Failure upgrading deps', ex ); | ||
|
||
await execAsync( `rm -rf ${ storage }` ); | ||
await exec_async( `rm -rf ${ storage }` ); | ||
|
||
process.exit( 1 ); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = upgradeDeps; | ||
module.exports = upgrade_deps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
const { exec } = require('child_process'); | ||
const { promisify } = require('util'); | ||
|
||
const execAsync = promisify( exec ); | ||
const exec_async = promisify( exec ); | ||
|
||
module.exports = execAsync; | ||
module.exports = exec_async; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters