Skip to content

Commit

Permalink
feat: add cli package for quick installation
Browse files Browse the repository at this point in the history
This will just install the package and will unload the burden of
npx-ing @wpackio/scripts and then installing it again.
  • Loading branch information
swashata committed Oct 22, 2018
1 parent 739f2e5 commit e09b70e
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ packages/*/lib
# Lerna
lerna-debug.log*

# Tryouts
.tryouts

## Standard gitignore ##
# Logs
logs
Expand Down
34 changes: 34 additions & 0 deletions packages/cli/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
*.un~
yarn.lock
src
flow-typed
coverage
decls
examples
40 changes: 40 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# `@wpackio/cli`

Stub README

// TODO
> Intro
## Installation

If using `yarn`

```bash
yarn add @wpackio/cli
```

or with `npm`

```bash
npm i @wpackio/cli
```

## Usage

// TODO
> Usage instruction
## Configuration

// TODO
> Configuration instruction
## Development

This package has the same `npm scripts` as this monorepo. These should be run
using `lerna run <script>`. More information can be found under [CONTRIBUTION.md](../../CONTRIBUTION.md).

- `build`: Use babel to build for nodejs 8.6+. Files inside `src` are compiled and put under `lib`. All type definitions are stripped and individual type declaration files are created.
- `prepare`: Run `build` after `yarn` and before `publish`.
- `lint`: Lint all files using tslint.
- `test`: Run tests on files using jest.
1 change: 1 addition & 0 deletions packages/cli/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../babel.config.js');
8 changes: 8 additions & 0 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const base = require('../../jest.base');
const pkg = require('./package.json');

module.exports = {
...base,
name: pkg.name,
displayName: pkg.name,
};
52 changes: 52 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@wpackio/cli",
"version": "0.0.1",
"description": "Commandline tool to quickly bootstrap wpackio-scripts to your project.",
"keywords": [
"wordpress",
"bundler",
"webpack",
"browser-sync",
"wordpress-bundler"
],
"bin": {
"wpackio-cli": "lib/index.js"
},
"preferGlobal": true,
"repository": "https://github.com/swashata/wp-webpack-script",
"homepage": "https://wpack.io",
"author": "Swashata Ghosh &lt;[email protected]&gt; (https://swas.io)",
"license": "MIT",
"private": false,
"dependencies": {
"chalk": "^2.4.1",
"commander": "^2.19.0",
"shelljs": "^0.8.2"
},
"engines": {
"node": ">=8.9.0"
},
"files": [
"lib"
],
"scripts": {
"clean": "rimraf './lib'",
"type-check": "tsc --noEmit",
"type-check:watch": "yarn type-check --watch",
"build:js": "babel ./src --out-dir lib --extensions '.ts,.tsx'",
"build:types": "tsc --emitDeclarationOnly",
"build": "yarn clean && yarn build:types && yarn build:js",
"lint": "tslint --project './tsconfig.json'",
"test": "jest --color",
"prepare": "cross-env NODE_ENV=production yarn build"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/chalk": "^2.2.0",
"@types/find-up": "^2.1.1",
"@types/node": "^10.12.0",
"@types/shelljs": "^0.8.0"
}
}
154 changes: 154 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/usr/bin/env node

/**
* CLI Tool for @wpackio/scripts
*
* This just uses npm or yarn to install @wpackio/scripts and runs
* the tooling. It shouldn't do anything else.
*
* It should have minimum dependency and all @types should go inside
* devDependency
*/

import chalk from 'chalk';
import program from 'commander';
import fs from 'fs';
import path from 'path';
import shelljs from 'shelljs';

const symErr = chalk.bgRed.black(' ERR ');
const symSucc = chalk.bgGreen.black(' SUC ');
const symWarn = chalk.bgYellow.black(' WAR ');
const symInfo = chalk.bgBlueBright.black(' INF ');
// Check for current version
const currentNodeVersion = process.versions.node;
const major = Number.parseInt(currentNodeVersion.split('.')[0], 10);

// Bail if node is lesser than 8
if (major < 8) {
console.error(
`${symErr} You are running Node ${major}. @wpackio/scripts requires Node 8 or higher.`
);
process.exit(1);
}

// Calculate basic things we need to bootstrap
const cwd = process.cwd();
const dirName = path.basename(cwd);

// Keep packageJson ready, in case we need to
const packageJson = {
name: dirName,
version: '0.1.0',
private: true,
};

// Auto-detect yarn usage
let isYarn: boolean;

try {
isYarn = fs.statSync(path.resolve(cwd, './yarn.lock')).isFile();
} catch (_) {
isYarn = false;
}

// Check if package.json is present
let hasPackageJson: boolean;
try {
hasPackageJson = fs.statSync(path.resolve(cwd, './package.json')).isFile();
} catch (_) {
hasPackageJson = false;
}

// Init program
interface Package {
version: string;
name: string;
private: boolean;
scripts?: { [x: string]: string };
}
interface ProgramOption {
client?: string;
}
const pkg = require('../package.json') as Package;

program
.version(pkg.version)
.description('Bootstrap @wpackio/scripts into your project.')
.option(
'-c, --client [client]',
`Which npm client to use. ${chalk.yellow('npm')} or ${chalk.yellow(
'yarn'
)}`
)
.action((options?: ProgramOption) => {
// Select the client
let client: 'yarn' | 'npm' = isYarn ? 'yarn' : 'npm';
if (options && options.client) {
if (options.client === 'yarn') {
client = 'yarn';
} else {
client = 'npm';
}
}
// create add depepdency command
const depCommand =
client === 'yarn'
? 'yarn add @wpackio/scripts'
: 'npm i @wpackio/scripts';
const bootCommand =
client === 'yarn' ? 'yarn bootstrap' : 'npm run bootstrap';
// Get path of packagejson
const pkgJsonPath = path.resolve(cwd, './package.json');
// Execute
console.log(
`${symInfo} bootstrapping @wpackio/scripts into your project`
);
console.log(`${symInfo} using client ${chalk.yellow(client)}`);
if (hasPackageJson) {
console.log(`${symInfo} ${chalk.yellow('package.json')} found`);
} else {
console.log(
`${symInfo} ${chalk.red(
'package.json'
)} not found, creating one`
);
fs.writeFileSync(pkgJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`${symSucc} created package.json file`);
}
console.log(`${symInfo} adding dependencies`);
console.log(`${symInfo} ${chalk.dim(depCommand)}`);
console.log(`${symInfo} this may take a while`);
if (shelljs.exec(depCommand).code !== 0) {
console.log(
`${symWarn} there was some error installing the dependencies`
);
console.log(
`${symWarn} please check and take corresponding actions`
);
console.log(
`${symWarn} trying to continue since this can be non-fatal`
);
} else {
console.log(`${symSucc} added dependencies`);
}
console.log(`${symInfo} adding scripts`);
// tslint:disable-next-line:non-literal-require
const pkgJson = require(pkgJsonPath) as Package;
pkgJson.scripts = pkgJson.scripts || {};
if (pkgJson.scripts) {
pkgJson.scripts.bootstrap = 'wpackio-scripts bootstrap';
} else {
pkgJson.scripts = { bootstrap: 'wpackio-scripts bootstrap' };
}
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
console.log(`${symSucc} all operations done.`);
console.log('\n\n');
console.log(`Please run the following command to get started`);
console.log('');
console.log(` ${chalk.yellow(bootCommand)}`);
console.log('');
console.log(`Happy programming!`);
});

program.parse(process.argv);
12 changes: 12 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./lib" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
},
"exclude": [
"__mocks__/**",
"__tests__/**",
"lib/**"
]
}
3 changes: 3 additions & 0 deletions packages/cli/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["../../tslint.json"]
}
Loading

0 comments on commit e09b70e

Please sign in to comment.