Socket
Socket
Sign inDemoInstall

gatsby-cli

Package Overview
Dependencies
Maintainers
1
Versions
1053
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gatsby-cli - npm Package Compare versions

Comparing version 1.0.12 to 1.1.0

create-cli.js

220

index.js
#!/usr/bin/env node
"use strict";
var _extends2 = require("babel-runtime/helpers/extends");
const resolveCwd = require(`resolve-cwd`);
const createCli = require(`./create-cli`);
const report = require(`./reporter`);
var _extends3 = _interopRequireDefault(_extends2);
global.Promise = require(`bluebird`);
require("core-js/modules/es6.typed.array-buffer");
const version = process.version;
const verDigit = Number(version.match(/\d+/)[0]);
require("core-js/modules/es6.typed.int8-array");
require("core-js/modules/es6.typed.uint8-array");
require("core-js/modules/es6.typed.uint8-clamped-array");
require("core-js/modules/es6.typed.int16-array");
require("core-js/modules/es6.typed.uint16-array");
require("core-js/modules/es6.typed.int32-array");
require("core-js/modules/es6.typed.uint32-array");
require("core-js/modules/es6.typed.float32-array");
require("core-js/modules/es6.typed.float64-array");
require("core-js/modules/es6.map");
require("core-js/modules/es6.set");
require("core-js/modules/es6.weak-map");
require("core-js/modules/es6.weak-set");
require("core-js/modules/es6.reflect.apply");
require("core-js/modules/es6.reflect.construct");
require("core-js/modules/es6.reflect.define-property");
require("core-js/modules/es6.reflect.delete-property");
require("core-js/modules/es6.reflect.get");
require("core-js/modules/es6.reflect.get-own-property-descriptor");
require("core-js/modules/es6.reflect.get-prototype-of");
require("core-js/modules/es6.reflect.has");
require("core-js/modules/es6.reflect.is-extensible");
require("core-js/modules/es6.reflect.own-keys");
require("core-js/modules/es6.reflect.prevent-extensions");
require("core-js/modules/es6.reflect.set");
require("core-js/modules/es6.reflect.set-prototype-of");
require("core-js/modules/es6.promise");
require("core-js/modules/es6.symbol");
require("core-js/modules/es6.function.name");
require("core-js/modules/es6.regexp.flags");
require("core-js/modules/es6.regexp.match");
require("core-js/modules/es6.regexp.replace");
require("core-js/modules/es6.regexp.split");
require("core-js/modules/es6.regexp.search");
require("core-js/modules/es6.array.from");
require("core-js/modules/es7.array.includes");
require("core-js/modules/es7.object.values");
require("core-js/modules/es7.object.entries");
require("core-js/modules/es7.object.get-own-property-descriptors");
require("core-js/modules/es7.string.pad-start");
require("core-js/modules/es7.string.pad-end");
require("regenerator-runtime/runtime");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var program = require(`commander`);
// babel-preset-env doesn't find this import if you
// use require() with backtick strings so use the es6 syntax
var packageJson = require(`./package.json`);
var path = require(`path`);
var _ = require(`lodash`);
var resolveCwd = require(`resolve-cwd`);
program.version(packageJson.version).usage(`[command] [options]`);
var inGatsbySite = false;
var localPackageJSON = void 0;
try {
localPackageJSON = require(path.resolve(`./package.json`));
if (localPackageJSON.dependencies && localPackageJSON.dependencies.gatsby || localPackageJSON.devDependencies && localPackageJSON.devDependencies.gatsby) {
inGatsbySite = true;
} else if (localPackageJSON.devDependencies && localPackageJSON.devDependencies.gatsby) {
inGatsbySite = true;
}
} catch (err) {
// ignore
if (verDigit < 4) {
report.panic(`Gatsby 1.0+ requires node.js v4 or higher (you have ${version}). \n` + `Upgrade node to the latest stable release.`);
}
var defaultHost = `localhost`;
Promise.onPossiblyUnhandledRejection(error => {
report.error(error);
throw error;
});
var directory = path.resolve(`.`);
var getSiteInfo = function getSiteInfo() {
var sitePackageJson = require(path.join(directory, `package.json`));
var browserslist = sitePackageJson.browserslist || [`> 1%`, `last 2 versions`, `IE >= 9`];
return { sitePackageJson, browserslist };
};
// If there's a package.json in the current directory w/ a gatsby dependency
// include the develop/build/serve commands. Otherwise, just the new.
if (inGatsbySite) {
program.command(`develop`).description(`Start development server. Watches files and rebuilds and hot reloads ` + `if something changes`) // eslint-disable-line max-len
.option(`-H, --host <url>`, `Set host. Defaults to ${defaultHost}`, defaultHost).option(`-p, --port <port>`, `Set port. Defaults to 8000`, `8000`).option(`-o, --open`, `Open the site in your browser for you.`).action(function (command) {
var developPath = resolveCwd(`gatsby/dist/utils/develop`);
var develop = require(developPath);
var _getSiteInfo = getSiteInfo(),
sitePackageJson = _getSiteInfo.sitePackageJson,
browserslist = _getSiteInfo.browserslist;
var p = (0, _extends3.default)({}, command, {
directory,
sitePackageJson,
browserslist
});
develop(p);
});
program.command(`build`).description(`Build a Gatsby project.`).option(`--prefix-paths`, `Build site with link paths prefixed (set prefix in your config).`).action(function (command) {
// Set NODE_ENV to 'production'
process.env.NODE_ENV = `production`;
var buildPath = resolveCwd(`gatsby/dist/utils/build`);
var build = require(buildPath);
var _getSiteInfo2 = getSiteInfo(),
sitePackageJson = _getSiteInfo2.sitePackageJson,
browserslist = _getSiteInfo2.browserslist;
var p = (0, _extends3.default)({}, command, {
directory,
sitePackageJson,
browserslist
});
build(p).then(function () {
console.log(`Done building in`, process.uptime(), `seconds`);
process.exit();
});
});
program.command(`serve`).description(`Serve built site.`).option(`-H, --host <url>`, `Set host. Defaults to ${defaultHost}`, defaultHost).option(`-p, --port <port>`, `Set port. Defaults to 9000`, `9000`).option(`-o, --open`, `Open the site in your browser for you.`).action(function (command) {
var servePath = resolveCwd(`gatsby/dist/utils/serve`);
var serve = require(servePath);
var _getSiteInfo3 = getSiteInfo(),
sitePackageJson = _getSiteInfo3.sitePackageJson,
browserslist = _getSiteInfo3.browserslist;
var p = (0, _extends3.default)({}, command, {
directory,
sitePackageJson,
browserslist
});
serve(p);
});
}
program.command(`new [rootPath] [starter]`).description(`Create new Gatsby project.`).action(function (rootPath, starter) {
var newCommand = require(`./new`);
newCommand(rootPath, starter);
process.on(`unhandledRejection`, error => {
// This will exit the process in newer Node anyway so lets be consistent
// across versions and crash
report.panic(`UNHANDLED REJECTION`, error);
});
program.on(`--help`, function () {
console.log(`To show subcommand help:
gatsby [command] -h
`);
process.on(`uncaughtException`, error => {
report.panic(`UNHANDLED EXCEPTION`, error);
});
// If the user types an unknown sub-command, just display the help.
var subCmd = process.argv.slice(2, 3)[0];
var cmds = _.map(program.commands, `_name`);
cmds = cmds.concat([`--version`, `-V`]);
if (!_.includes(cmds, subCmd)) {
program.help();
} else {
program.parse(process.argv);
}
createCli(process.argv, {
develop: () => require(resolveCwd(`gatsby/dist/commands/develop`)),
build: () => require(resolveCwd(`gatsby/dist/commands/build`)),
serve: () => require(resolveCwd(`gatsby/dist/commands/serve`))
});
"use strict";
var _child_process = require("child_process");
const { execSync } = require(`child_process`);
const execa = require(`execa`);
const hostedGitInfo = require(`hosted-git-info`);
const fs = require(`fs-extra`);
const sysPath = require(`path`);
const report = require(`./reporter`);
var _hostedGitInfo = require("hosted-git-info");
const spawn = cmd => {
const [file, ...args] = cmd.split(/\s+/);
return execa(file, args, { stdio: `inherit` });
};
var _hostedGitInfo2 = _interopRequireDefault(_hostedGitInfo);
var _fsExtra = require("fs-extra");
var _fsExtra2 = _interopRequireDefault(_fsExtra);
var _path = require("path");
var _path2 = _interopRequireDefault(_path);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* weak */
var logger = console;
// Checks the existence of yarn package

@@ -27,5 +20,5 @@ // We use yarnpkg instead of yarn to avoid conflict with Hadoop yarn

// Returns true if yarn exists, false otherwise
var shouldUseYarn = function shouldUseYarn() {
const shouldUseYarn = () => {
try {
(0, _child_process.execSync)(`yarnpkg --version`, { stdio: `ignore` });
execSync(`yarnpkg --version`, { stdio: `ignore` });
return true;

@@ -37,115 +30,70 @@ } catch (e) {

// Executes `npm install` and `bower install` in rootPath.
//
// rootPath - String. Path to directory in which command will be executed.
// callback - Function. Takes stderr and stdout of executed process.
//
// Returns nothing.
var install = function install(rootPath, callback) {
var prevDir = process.cwd();
logger.log(`Installing packages...`);
// Executes `npm install` or `yarn install` in rootPath.
const install = async rootPath => {
const prevDir = process.cwd();
report.info(`Installing packages...`);
process.chdir(rootPath);
var installCmd = shouldUseYarn() ? `yarnpkg` : `npm install`;
(0, _child_process.exec)(installCmd, function (error, stdout, stderr) {
try {
let cmd = shouldUseYarn() ? spawn(`yarnpkg`) : spawn(`npm install`);
await cmd;
} finally {
process.chdir(prevDir);
if (stdout) console.log(stdout.toString());
if (error !== null) {
var msg = stderr.toString();
callback(new Error(msg));
}
callback(null, stdout);
});
}
};
var ignored = function ignored(path) {
return !/^\.(git|hg)$/.test(_path2.default.basename(path));
};
const ignored = path => !/^\.(git|hg)$/.test(sysPath.basename(path));
// Copy starter from file system.
//
// starterPath - String, file system path from which files will be taken.
// rootPath - String, directory to which starter files will be copied.
// callback - Function.
//
// Returns nothing.
var copy = function copy(starterPath, rootPath, callback) {
var copyDirectory = function copyDirectory() {
_fsExtra2.default.copy(starterPath, rootPath, { filter: ignored }, function (error) {
if (error !== null) return callback(new Error(error));
logger.log(`Created starter directory layout`);
install(rootPath, callback);
return false;
});
};
const copy = async (starterPath, rootPath) => {
// Chmod with 755.
// 493 = parseInt('755', 8)
_fsExtra2.default.mkdirp(rootPath, { mode: 493 }, function (error) {
if (error !== null) callback(new Error(error));
return _fsExtra2.default.exists(starterPath, function (exists) {
if (!exists) {
var chmodError = `starter ${starterPath} doesn't exist`;
return callback(new Error(chmodError));
}
logger.log(`Copying local starter to ${rootPath} ...`);
await fs.mkdirp(rootPath, { mode: 493 });
copyDirectory();
return true;
});
});
};
if (!fs.existsSync(starterPath)) {
throw new Error(`starter ${starterPath} doesn't exist`);
}
report.info(`Creating new site from local starter: ${starterPath}`);
// Clones starter from URI.
//
// address - String, URI. https:, github: or git: may be used.
// rootPath - String, directory to which starter files will be copied.
// callback - Function.
//
// Returns nothing.
var clone = function clone(hostInfo, rootPath, callback) {
var url = hostInfo.git({ noCommittish: true });
var branch = hostInfo.committish ? `-b ${hostInfo.committish}` : ``;
report.log(`Copying local starter to ${rootPath} ...`);
logger.log(`Cloning git repo ${url} to ${rootPath}...`);
var cmd = `git clone ${branch} ${url} ${rootPath} --single-branch`;
await fs.copy(starterPath, rootPath, { filter: ignored });
(0, _child_process.exec)(cmd, function (error, stdout, stderr) {
if (error !== null) {
return callback(new Error(`Git clone error: ${stderr.toString()}`));
}
logger.log(`Created starter directory layout`);
return _fsExtra2.default.remove(_path2.default.join(rootPath, `.git`), function (removeError) {
if (error !== null) return callback(new Error(removeError));
install(rootPath, callback);
return true;
});
});
report.success(`Created starter directory layout`);
await install(rootPath);
return true;
};
// Main function that clones or copies the starter.
//
// starter - String, file system path or URI of starter.
// rootPath - String, directory to which starter files will be copied.
// callback - Function.
//
// Returns nothing.
var initStarter = function initStarter(starter) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new Promise(function (resolve, reject) {
var callback = function callback(err, value) {
return err ? reject(err) : resolve(value);
};
// Clones starter from URI.
const clone = async (hostInfo, rootPath) => {
const url = hostInfo.git({ noCommittish: true });
const branch = hostInfo.committish ? `-b ${hostInfo.committish}` : ``;
var cwd = process.cwd();
var rootPath = options.rootPath || cwd;
if (options.logger) logger = options.logger;
report.info(`Creating new site from git: ${url}`);
if (_fsExtra2.default.existsSync(_path2.default.join(rootPath, `package.json`))) throw new Error(`Directory ${rootPath} is already an npm project`);
await spawn(`git clone ${branch} ${url} ${rootPath} --single-branch`);
var hostedInfo = _hostedGitInfo2.default.fromUrl(starter);
report.success(`Created starter directory layout`);
if (hostedInfo) clone(hostedInfo, rootPath, callback);else copy(starter, rootPath, callback);
});
await fs.remove(sysPath.join(rootPath, `.git`));
await install(rootPath);
};
module.exports = initStarter;
/**
* Main function that clones or copies the starter.
*/
module.exports = async (starter, options = {}) => {
const rootPath = options.rootPath || process.cwd();
if (fs.existsSync(sysPath.join(rootPath, `package.json`))) {
report.panic(`Directory ${rootPath} is already an npm project`);
return;
}
const hostedInfo = hostedGitInfo.fromUrl(starter);
if (hostedInfo) await clone(hostedInfo, rootPath);else await copy(starter, rootPath);
};
"use strict";
/* weak */
var logger = require(`tracer`).colorConsole();
const logger = require(`tracer`).colorConsole();
var initStarter = require(`./init-starter`);
const initStarter = require(`./init-starter`);
module.exports = function (rootPath) {
var starter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : `gatsbyjs/gatsby-starter-default`;
initStarter(starter, { rootPath, logger }).catch(function (error) {
return logger.error(error);
});
module.exports = (rootPath, starter = `gatsbyjs/gatsby-starter-default`) => {
initStarter(starter, { rootPath, logger }).catch(error => logger.error(error));
};
{
"name": "gatsby-cli",
"description": "Gatsby command-line interface for creating new sites and running Gatsby commands",
"version": "1.0.12",
"version": "1.1.0",
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"bin": {
"gatsby": "./index.js"
"gatsby": "lib/index.js"
},
"dependencies": {
"babel-runtime": "^6.25.0",
"commander": "^2.11.0",
"common-tags": "^1.4.0",
"convert-hrtime": "^2.0.0",
"core-js": "^2.5.0",
"execa": "^0.8.0",
"fs-extra": "^4.0.1",
"hosted-git-info": "^2.5.0",
"lodash": "^4.17.4",
"pretty-error": "^2.1.1",
"resolve-cwd": "^2.0.0",
"tracer": "^0.8.9"
"yargs": "^8.0.2",
"yurnalist": "^0.2.1"
},

@@ -26,7 +30,7 @@ "devDependencies": {

"license": "MIT",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__"
"build": "babel src --out-dir lib --ignore __tests__",
"watch": "babel -w src --out-dir lib --ignore __tests__"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc