Last active
April 5, 2024 12:22
-
-
Save nmicht/fe8df38d3e0323de7cc3bcd21d4928c6 to your computer and use it in GitHub Desktop.
Script to override the npm init. Prompts for the normal stuff, but also creates a github repository
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 script can be used to override the current behave of `npm init` | |
* Can have a set of "defaults" values and also, will include functionality | |
* to create a Github repository for the project. | |
* | |
* You can set it: | |
* npm config set init-module YOUR-PATH/.npm-init.js | |
* | |
* Note: This is a work in progress, so there are some failures, mostly because | |
* npm is using promzard for the prompts, and it doesn't have async methods, so | |
* there are not enough flexibility to work on this script. | |
* - The github repository will be created with the basename even if the user | |
* select other name. | |
* - In case the folder already have a package.json or a git project, this will | |
* fail. | |
* - If the user select to not create a repository, even so will add the values | |
* to the package json. | |
* | |
* To disable you should execute: | |
* npm config set init-module | |
* | |
* Made with <3 by nmicht during RC bootcamp | |
*/ | |
const cp = require('child_process'); | |
const USER = 'YOUR-USER'; | |
const NAME = 'YOUR-NAME'; | |
const EMAIL = 'YOUR-EMAIL'; | |
const WEBSITE = 'YOUR-WEBSITE'; | |
const TOKEN = 'YOUR-TOKEN'; | |
const GITURL = `https://github.com/${USER}/${basename}.git` | |
module.exports = { | |
name: prompt('name', basename || package.name), | |
version: prompt('version', '0.0.1'), | |
description: prompt('description', ''), | |
private: prompt('private', 'true', function(res) { | |
return priv = (typeof res === 'boolean') ? res : !!res.match('true') | |
}), | |
main: prompt('entry point', 'main.js'), | |
keywords: prompt(function (s) { return s.split(/\s+/) }), | |
license: prompt('license', 'MIT'), | |
author: prompt('author', `${NAME} <${EMAIL}> (${WEBSITE})`), | |
create: prompt('create github repo', 'yes', function(res) { | |
res = res.indexOf('y') !== -1 ? true : false; | |
if(res){ | |
let gitCreateCommand = `curl -i -H "Authorization: token ${TOKEN}" -d '{"name": "${basename}", "private": "${(priv) ? 'true' : 'false'}}' https://api.github.com/user/repos`; | |
console.log('... Initializing Git repo ...\n'); | |
cp.execSync('git init', {stdio: 'inherit'}); | |
console.log('... Creating Github repo ...\n'); | |
cp.execSync(gitCreateCommand, {stdio: 'inherit'}); | |
console.log('... Adding git remote ...\n'); | |
cp.execSync(`git remote add origin ${GITURL}`, {stdio: 'inherit'}); | |
} | |
return undefined; | |
}), | |
repository: { | |
type: 'git', | |
url: `git+${GITURL}`, | |
}, | |
bugs: { | |
url: `${GITURL}/issues`, | |
}, | |
homepage: GITURL, | |
cleanup: function(cb){ | |
cb(null, undefined) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment