├── .npmrc ├── .gitattributes ├── README.md ├── .github └── workflows │ ├── ci.yml │ └── publish.yml ├── package.json ├── LICENSE ├── .gitignore ├── cli.js └── test.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # is-ci-cli ![CI](https://github.com/YellowKirby/is-ci-cli/workflows/CI/badge.svg) 2 | 3 | Run different npm scripts in a CI environment 4 | 5 | ## Install 6 | 7 | Ensure you have [Node.js](https://nodejs.org) version 8+ installed. Then run the following: 8 | 9 | ```sh 10 | npm install --dev is-ci-cli 11 | ``` 12 | 13 | ## Usage 14 | 15 | In your package.json: 16 | 17 | ```js 18 | "scripts": { 19 | "test": "is-ci-cli test:ci test:local" 20 | } 21 | ``` 22 | 23 | When in a CI environment (as detected by 24 | [is-ci](https://github.com/watson/is-ci)), this runs the first argument as an 25 | npm (or yarn) script. Otherwise, run the 2nd argument (if provided) 26 | 27 | ## License 28 | 29 | MIT 30 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: CI 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | test: 14 | name: Test on node ${{ matrix.node-version }} and ${{ matrix.os }} 15 | runs-on: ${{ matrix.os }} 16 | 17 | strategy: 18 | matrix: 19 | node-version: [8, 10, 12, 14, 15] 20 | os: [ubuntu-latest, windows-latest, macos-latest] 21 | 22 | steps: 23 | - uses: actions/checkout@v2 24 | - uses: actions/setup-node@v2-beta 25 | name: Use Node.js ${{ matrix.node-version }} 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | - run: npm i 29 | - run: npm test 30 | - run: npm run ci:test 31 | - run: yarn ci:test 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-ci-cli", 3 | "version": "2.2.0", 4 | "description": "Run different npm scripts in a CI environment", 5 | "license": "MIT", 6 | "repository": "YellowKirby/is-ci-cli", 7 | "bin": { 8 | "is-ci": "cli.js", 9 | "is-ci-cli": "cli.js" 10 | }, 11 | "engines": { 12 | "node": ">=8" 13 | }, 14 | "files": [ 15 | "cli.js" 16 | ], 17 | "keywords": [ 18 | "ci", 19 | "cli", 20 | "npm-scripts" 21 | ], 22 | "dependencies": { 23 | "cross-spawn": "^7.0.0", 24 | "is-ci": "^2.0.0" 25 | }, 26 | "devDependencies": { 27 | "ava": "^0.19.1", 28 | "sinon": "^8.1.1", 29 | "xo": "^0.18.2" 30 | }, 31 | "scripts": { 32 | "lint": "xo", 33 | "pretest": "npm run lint", 34 | "test": "ava", 35 | "ci:test": "node cli.js ci:check-args ci:fail --arg1", 36 | "ci:fail": "echo \"CI ran incorrect run-script, failing\"; exit 1;", 37 | "ci:check-args": "node -e \"assert(process.argv[1] === '--arg1', 'process.argv[1]' + ' should match --arg1')\" --" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages 3 | 4 | name: Publish 5 | 6 | on: 7 | release: 8 | types: [created] 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: actions/setup-node@v1 16 | with: 17 | node-version: 12 18 | - run: npm i 19 | - run: npm test 20 | - run: npm run ci:test 21 | - run: yarn ci:test 22 | 23 | publish-npm: 24 | needs: build 25 | runs-on: ubuntu-latest 26 | steps: 27 | - uses: actions/checkout@v2 28 | - uses: actions/setup-node@v1 29 | with: 30 | node-version: 12 31 | registry-url: https://registry.npmjs.org/ 32 | - run: npm i 33 | - run: npm publish 34 | env: 35 | NODE_AUTH_TOKEN: ${{secrets.npm_token}} 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 YellowKirby 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | .idea 60 | 61 | # Lock files 62 | package-lock.json 63 | yarn.lock 64 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 'use strict'; 4 | 5 | process.on('unhandledRejection', err => { 6 | throw err; 7 | }); 8 | 9 | const crossSpawn = require('cross-spawn'); 10 | const isCi = require('is-ci'); 11 | 12 | function isYarn(npmExec) { 13 | return npmExec.includes('yarn'); 14 | } 15 | 16 | function getScriptArgs(args, npmExec) { 17 | const rawArgs = args.slice(2); 18 | // Npm requires script arguments with dashes, like so: `npm run test -- --watch` 19 | // Yarn results in a deprecation warning in this case, so dashes need to be skipped 20 | const shouldPrefixArgs = rawArgs.length > 0 && !isYarn(npmExec); 21 | return shouldPrefixArgs ? ['--', ...rawArgs] : rawArgs; 22 | } 23 | 24 | function run(args, isCi, spawn, npmExec = 'npm') { 25 | const script = isCi ? args[0] : args[1]; 26 | const scriptArgs = getScriptArgs(args, npmExec); 27 | 28 | if (script) { 29 | return spawn( 30 | npmExec, 31 | ['run', script, ...scriptArgs], 32 | { 33 | stdio: 'inherit' 34 | } 35 | ); 36 | } 37 | } 38 | 39 | module.exports = run; 40 | 41 | if (require.main === module) { 42 | const child = run(process.argv.slice(2), isCi, crossSpawn, process.env.npm_execpath); 43 | if (child) { 44 | child.on('exit', process.exit); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import sinon from 'sinon'; 3 | import run from './cli'; 4 | 5 | /** @type {sinon.SinonSpy} */ 6 | let spawn; 7 | 8 | test.before('fake spawn', () => { 9 | spawn = sinon.fake((name, args) => args); 10 | }); 11 | 12 | test.afterEach('reset fake', () => { 13 | spawn.resetHistory(); 14 | }); 15 | 16 | test('runs first argument when in CI environment', t => { 17 | run(['first', 'second'], true, spawn); 18 | t.true(spawn.returned(['run', 'first'])); 19 | }); 20 | 21 | test('runs second argument when not in CI environment', t => { 22 | run(['first', 'second'], false, spawn); 23 | t.true(spawn.returned(['run', 'second'])); 24 | }); 25 | 26 | test('runs nothing if no second argument and not in CI environment', t => { 27 | run(['first'], false, spawn); 28 | t.false(spawn.called); 29 | }); 30 | 31 | test('runs script with additional arguments if provided', t => { 32 | run(['first', 'second', '--test', 'value'], true, spawn, 'npm'); 33 | t.true(spawn.lastCall.returned(['run', 'first', '--', '--test', 'value'])); 34 | 35 | run(['first', 'second', '--test', 'value'], false, spawn, 'npm'); 36 | t.true(spawn.lastCall.returned(['run', 'second', '--', '--test', 'value'])); 37 | 38 | run(['first', 'second', '--test', 'value'], true, spawn, 'yarn'); 39 | t.true(spawn.lastCall.returned(['run', 'first', '--test', 'value'])); 40 | 41 | run(['first', 'second', '--test', 'value'], false, spawn, 'yarn'); 42 | t.true(spawn.lastCall.returned(['run', 'second', '--test', 'value'])); 43 | }); 44 | --------------------------------------------------------------------------------