Skip to content

Commit

Permalink
feat: provide basic clone functionality
Browse files Browse the repository at this point in the history
First version of script to clone the React starter kit by default.

closes #7
  • Loading branch information
juancarlosfarah committed Feb 5, 2019
1 parent 6f02df5 commit cadd211
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 15 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
},
"dependencies": {
"@babel/polyfill": "7.2.5",
"execa": "1.0.0",
"fs-exists-cached": "1.0.0",
"fs-extra": "7.0.1",
"hosted-git-info": "2.7.1",
"yargs": "12.0.5"
}
}
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// todo: remove when more items are exported
// eslint-disable-next-line import/prefer-default-export
export const DEFAULT_STARTER = 'graasp/graasp-app-starter-react';
23 changes: 22 additions & 1 deletion src/createCli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import yargs from 'yargs';
import initStarter from './initStarter';
import { DEFAULT_STARTER } from './config';

const promisify = fn => (...args) => {
Promise.resolve(fn(...args)).then(
() => process.exit(0),
// err => report.panic(err)
);
};

const createCli = (argv) => {
const cli = yargs();
Expand All @@ -22,8 +31,20 @@ const createCli = (argv) => {

return cli
.command({
command: 'new [rootPath] [starter]',
command: 'new [projectDirectory]',
desc: 'Create new Graasp app.',
builder: _ => _.option('s', {
alias: 'starter',
type: 'string',
default: DEFAULT_STARTER,
describe: `Set starter. Defaults to ${DEFAULT_STARTER}`,
}),
handler: promisify(
({
starter,
projectDirectory,
}) => initStarter(projectDirectory, { starter }),
),
})
.wrap(cli.terminalWidth())
.demandCommand(1, 'Pass --help to see all available commands and options.')
Expand Down
82 changes: 82 additions & 0 deletions src/initStarter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import path from 'path';
import execSync from 'child_process';
import execa from 'execa';
import fs from 'fs-extra';
import HostedGitInfo from 'hosted-git-info';
import { sync as existsSync } from 'fs-exists-cached';
import { DEFAULT_STARTER } from './config';

// use execa to spawn a better child process
const spawn = (cmd) => {
const [file, ...args] = cmd.split(/\s+/);
return execa(file, args, { stdio: 'inherit' });
};

/**
* checks for the existence of yarn package
* use yarnpkg instead of yarn to avoid conflict with Hadoop yarn
* see: https://github.com/yarnpkg/yarn/issues/673
* @returns {boolean} true if yarn exists, false otherwise
*/
const shouldUseYarn = () => {
try {
execSync('yarnpkg --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
};

// executes `npm install` or `yarn install` in rootPath
const install = async (rootPath) => {
const prevDir = process.cwd();

console.log('installing packages...');
process.chdir(rootPath);

try {
const cmd = shouldUseYarn() ? spawn('yarnpkg') : spawn('npm install');
await cmd;
} finally {
process.chdir(prevDir);
}
};

const clone = async (hostInfo, rootPath) => {
let url;
// let people use private repos accessed over ssh
if (hostInfo.getDefaultRepresentation() === 'sshurl') {
url = hostInfo.ssh({ noCommittish: true });
// otherwise default to normal git syntax
} else {
url = hostInfo.https({ noCommittish: true, noGitPlus: true });
}

const branch = hostInfo.committish ? `-b ${hostInfo.committish}` : '';

console.log(`creating new site from git: ${url}`);

await spawn(`git clone ${branch} ${url} ${rootPath} --single-branch`);

console.log('created starter directory layout');

await fs.remove(path.join(rootPath, '.git'));

await install(rootPath);
};

const initStarter = async (projectDirectory, options = {}) => {
const rootPath = projectDirectory || process.cwd();

const { starter = DEFAULT_STARTER } = options;

if (existsSync(path.join(rootPath, 'package.json'))) {
console.error(`directory ${rootPath} is already an npm project`);
return false;
}

const hostedInfo = HostedGitInfo.fromUrl(starter);
return clone(hostedInfo, rootPath);
};

export default initStarter;
54 changes: 40 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1838,26 +1838,26 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
integrity sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=

execa@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777"
integrity sha1-lEvs00zEHuMqY6n68nrVpl/Fl3c=
execa@1.0.0, execa@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-1.0.0.tgz#c6236a5bb4df6d6f15e88e7f017798216749ddd8"
integrity sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==
dependencies:
cross-spawn "^5.0.1"
get-stream "^3.0.0"
cross-spawn "^6.0.0"
get-stream "^4.0.0"
is-stream "^1.1.0"
npm-run-path "^2.0.0"
p-finally "^1.0.0"
signal-exit "^3.0.0"
strip-eof "^1.0.0"

execa@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-1.0.0.tgz#c6236a5bb4df6d6f15e88e7f017798216749ddd8"
integrity sha512-adbxcyWV46qiHyvSp50TKt05tB4tK3HcmF7/nxfAdhnox83seTDbwnaqKO4sXRy7roHAIFqJP/Rw/AuEbX61LA==
execa@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777"
integrity sha1-lEvs00zEHuMqY6n68nrVpl/Fl3c=
dependencies:
cross-spawn "^6.0.0"
get-stream "^4.0.0"
cross-spawn "^5.0.1"
get-stream "^3.0.0"
is-stream "^1.1.0"
npm-run-path "^2.0.0"
p-finally "^1.0.0"
Expand Down Expand Up @@ -2014,6 +2014,20 @@ fs-access@^1.0.0:
dependencies:
null-check "^1.0.0"

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs-exists-cached/-/fs-exists-cached-1.0.0.tgz#cf25554ca050dc49ae6656b41de42258989dcbce"
integrity sha1-zyVVTKBQ3EmuZla0HeQiWJidy84=

[email protected]:
version "7.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9"
integrity sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==
dependencies:
graceful-fs "^4.1.2"
jsonfile "^4.0.0"
universalify "^0.1.0"

fs-minipass@^1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.5.tgz#06c277218454ec288df77ada54a03b8702aacb9d"
Expand Down Expand Up @@ -2177,7 +2191,7 @@ globals@^11.1.0, globals@^11.7.0:
resolved "https://registry.yarnpkg.com/globals/-/globals-11.10.0.tgz#1e09776dffda5e01816b3bb4077c8b59c24eaa50"
integrity sha512-0GZF1RiPKU97IHUO5TORo9w1PwrH/NBPl+fS7oMLdaTRiYmYbwK4NWoZWrAdd0/abG9R2BU+OiwyQpTpE6pdfQ==

graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.3:
graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.3, graceful-fs@^4.1.6:
version "4.1.15"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00"
integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==
Expand Down Expand Up @@ -2253,7 +2267,7 @@ has@^1.0.1, has@^1.0.3:
dependencies:
function-bind "^1.1.1"

hosted-git-info@^2.1.4:
hosted-git-info@2.7.1, hosted-git-info@^2.1.4:
version "2.7.1"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.7.1.tgz#97f236977bd6e125408930ff6de3eec6281ec047"
integrity sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w==
Expand Down Expand Up @@ -2669,6 +2683,13 @@ json5@^2.1.0:
dependencies:
minimist "^1.2.0"

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
integrity sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=
optionalDependencies:
graceful-fs "^4.1.6"

jsonparse@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
Expand Down Expand Up @@ -4298,6 +4319,11 @@ union-value@^1.0.0:
is-extendable "^0.1.1"
set-value "^0.4.3"

universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==

unset-value@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unset-value/-/unset-value-1.0.0.tgz#8376873f7d2335179ffb1e6fc3a8ed0dfc8ab559"
Expand Down

0 comments on commit cadd211

Please sign in to comment.