Skip to content

Commit b2569c2

Browse files
Initial commit
1 parent ea23a0c commit b2569c2

8 files changed

+732
-1
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Tom Rowe
3+
Copyright (c) 2020 Thomas Rowe
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

git-npx.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import arg from 'arg';
2+
import path from 'path';
3+
import fs from 'fs';
4+
import chalk from 'chalk';
5+
import rimraf from 'rimraf';
6+
import os from 'os';
7+
import {spawnSync} from 'child_process';
8+
import AdmZip from 'adm-zip';
9+
import request from 'request';
10+
import camel from './scripts/camelMessages';
11+
import brandMessage from './scripts/brandMessage';
12+
import nextSteps from './scripts/nextStepsMessage';
13+
const packageJson = require('./package.json');
14+
15+
function parseArguments(rawArgs) {
16+
const variables = {
17+
'--help': Boolean,
18+
'-h': '--help',
19+
'--version': Boolean,
20+
'-v': '--version',
21+
};
22+
const args = arg(variables, { argv: rawArgs.slice(2) });
23+
return {
24+
help: args['--help'] || false,
25+
version: args['--version'] || false,
26+
projectName: args._[0],
27+
};
28+
}
29+
30+
function validateProjectName(projectName) {
31+
if (!projectName) {
32+
camel.error('Project name was not passed.');
33+
return false;
34+
} else if (!/^[A-Za-z\-]+$/.test(projectName)) {
35+
camel.error('Project names should only have lowercase letters and dashes.');
36+
return false;
37+
}
38+
39+
//Once validated, confirm the folder doesn't already exist
40+
const projectPath = path.join(process.cwd(), projectName);
41+
42+
if (fs.existsSync(projectName)) {
43+
camel.error(`A project folder with that name already exists, at '${projectPath}'.`);
44+
return false;
45+
}
46+
47+
return true;
48+
}
49+
50+
export async function cli(args) {
51+
try{
52+
let options = parseArguments(args);
53+
if(options.help) {
54+
console.log(`You don\'t need help! Just run ${chalk.hex('#86BC25')('npx git-npx <project-name>')}!`);
55+
} else if (options.version) {
56+
console.log(packageJson.version);
57+
} else {
58+
let isValid = validateProjectName(options.projectName);
59+
if (isValid) {
60+
brandMessage();
61+
const destination = path.join(process.cwd(), options.projectName);
62+
fs.mkdirSync(destination);
63+
console.log(chalk.cyan(`Copying project template to ${destination}...`));
64+
try {
65+
const gitUrl = packageJson.repository.url.replace('.git', '/zipball/master')
66+
request.get({url: gitUrl, encoding: null}, (err, res, body) => {
67+
const zip = new AdmZip(body);
68+
const files = zip.getEntries();
69+
files.forEach(file => {
70+
//file.entryName = file.entryName.substring(file.entryName.indexOf('/') + 1, file.entryName.length);
71+
zip.extractEntryTo(file, destination, false, true);
72+
73+
})
74+
// zip.extractAllTo(destination, false, true);
75+
// console.log(chalk.green('Copy complete!'));
76+
// console.log('Running npm installation...');
77+
// const npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm';
78+
// spawnSync(npmCmd, ['i', '--no-package-lock'], { env: process.env, cwd: destination, stdio: 'inherit' });
79+
// camel.success('Installation complete!');
80+
// nextSteps();
81+
});
82+
} catch (e) {
83+
camel.error(e);
84+
}
85+
}
86+
}
87+
} catch(e) {
88+
camel.error(e);
89+
process.exit(1);
90+
}
91+
}
92+

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env node
2+
3+
require = require('esm')(module);
4+
require('./git-npx').cli(process.argv);

0 commit comments

Comments
 (0)