UNPKG

@interaktiv/dia-scripts

Version:

CLI toolbox with common scripts for most sort of projects at DIA

103 lines (87 loc) 2.71 kB
"use strict"; const path = require('path'); const spawn = require('cross-spawn'); const chalk = require('chalk'); const exit = require('exit'); const isCI = require('is-ci'); const { parseEnv, SCRIPTS_PACKAGE_NAME } = require('../utils'); const EXIT_CODE_SUCCESS = 0; function isInGitRepository(appPath = process.cwd()) { const result = spawn.sync('git', ['rev-parse', '--is-inside-work-tree'], { stdio: 'ignore', cwd: appPath }); return result.status === EXIT_CODE_SUCCESS; } function tryGitInit(appPath) { const gitVersionResult = spawn.sync('git', ['--version'], { stdio: 'ignore', cwd: appPath }); if (gitVersionResult.status > EXIT_CODE_SUCCESS) return false; if (isInGitRepository(appPath)) return false; const gitInitResult = spawn.sync('git', ['init'], { stdio: 'ignore', cwd: appPath }); if (gitInitResult.status > EXIT_CODE_SUCCESS) return false; return true; // TODO: Do we really need this? // spawn.sync('git', ['add', '-A'], { stdio: 'ignore', cwd: appPath }); // const gitCommitResult = spawn.sync( // 'git', // ['commit', '-m', `"feat: initial commit from ${SCRIPTS_PACKAGE_NAME}"`], // { // stdio: 'ignore', // cwd: appPath, // }, // ); // if (gitCommitResult.status === EXIT_CODE_SUCCESS) return true; // If we successfully initialized but couldn't commit, // maybe the commit author config is not set. // In the future, we might supply our own committer // like Ember CLI does, but for now, let's just // remove the Git files to avoid a half-done state. // try { // // On directories unlinkSync() does not work // fs.removeSync(path.join(appPath, '.git')); // } catch (removeErr) { // // Ignore. // } // return false; } (function () { if (isCI) { console.log(''); console.log('CI detected, skipping init.'); exit(0); return; } const cwd = parseEnv('INIT_CWD', process.cwd()); const appPkgPath = path.join(cwd, 'package.json'); let appPkg; try { appPkg = require(appPkgPath); } catch (err) {// Ignore error } if (appPkg == null) { console.log(''); console.log(chalk.yellow('Could not detect working directory, aborting git initialization. Please run `dia-scripts git` after install.')); exit(404); return; } if (appPkg.name === SCRIPTS_PACKAGE_NAME) { console.log(''); console.log(chalk.yellow('Running self, no need to init, skipping')); exit(EXIT_CODE_SUCCESS); return; } const appDirectory = path.dirname(appPkgPath); // Init a git repo if (tryGitInit(appDirectory)) { console.log(''); console.log(chalk.blue('Initialized a git repository')); console.log(''); } })();