From 1a4a7f828ba55fd8f5aee67dcbd7fa5147b96131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Nikoli=C4=87?= Date: Fri, 6 Nov 2020 12:04:49 +0100 Subject: [PATCH] Add support for GitHub Actions Closes #155. --- generators/app/index.js | 27 +++++++++++++++++-- generators/app/templates/README.md | 4 +-- .../app/templates/github/workflows/ci.yml | 21 +++++++++++++++ generators/app/templates/karma.conf.js | 2 +- test/index.js | 16 +++++++++++ 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 generators/app/templates/github/workflows/ci.yml diff --git a/generators/app/index.js b/generators/app/index.js index 635588e..688c34e 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -140,6 +140,23 @@ module.exports = class extends Generator { message: 'Do you have automated tests?', default: true }, + { + type: 'list', + name: 'ciService', + message: 'What CI service do you want to test on?', + default: 'travis', + choices: [ + { + name: 'Travis', + value: 'travis' + }, + { + name: 'GitHub', + value: 'github' + } + ], + when: (answers) => answers.automatedTests + }, { type: 'list', name: 'browserTestType', @@ -378,7 +395,8 @@ module.exports = class extends Generator { bundlingTool: answers.bundlingTool, sourceMaps: answers.sourceMaps, prettier: answers.prettier, - lowestIEVersion: lowestIEVersion + lowestIEVersion: lowestIEVersion, + ciService: answers.ciService }; this.tpl = Object.assign({}, tpl, { @@ -445,7 +463,11 @@ module.exports = class extends Generator { } if (answers.automatedTests) { - cp('travis.yml', '.travis.yml'); + if (answers.ciService === 'travis') { + cp('travis.yml', '.travis.yml'); + } else { + cp('github', '.github'); + } if (answers.browserModule) { if (!answers.sassModule) { cp( @@ -475,6 +497,7 @@ module.exports = class extends Generator { } } else { rm('.travis.yml'); + rm('.github'); rm('test/index.js'); rm('test/automated'); rm('karma.conf.js'); diff --git a/generators/app/templates/README.md b/generators/app/templates/README.md index 39e8cf1..80ee1cf 100644 --- a/generators/app/templates/README.md +++ b/generators/app/templates/README.md @@ -129,8 +129,8 @@ MIT © [<%= humanName %>](<%= website %>)<% if ( automatedTests ) { %><% if ( pr <% } %> -[ci]: https://travis-ci.com/<%= username %>/<%= cleanModuleName %> -[ci-img]: https://travis-ci.com/<%= username %>/<%= cleanModuleName %>.svg?branch=master<% } %><% if ( cloudBrowsers && ( (automatedTests && browserModule && !sassModule) || integrationTests ) ) { %> +[ci]: <% if ( ciService === 'travis' ) { %>https://travis-ci.com/<%= username %>/<%= cleanModuleName %><% } else { %>https://github.com/<%= username %>/<%= cleanModuleName %>/actions?query=workflow%3ACI<% } %> +[ci-img]: <% if ( ciService === 'travis' ) { %>https://travis-ci.com/<%= username %>/<%= cleanModuleName %>.svg?branch=master% } else { %>https://github.com/<%= username %>/<%= cleanModuleName %>/workflows/CI/badge.svg?branch=master<% } %><% } %><% if ( cloudBrowsers && ( (automatedTests && browserModule && !sassModule) || integrationTests ) ) { %> [browserstack]: https://www.browserstack.com/ [browserstack-img]: https://www.browserstack.com/automate/badge.svg?badge_key=<% } %><% if ( codeCoverageService ) { %> [coverage]: https://coveralls.io/github/<%= username %>/<%= cleanModuleName %>?branch=master diff --git a/generators/app/templates/github/workflows/ci.yml b/generators/app/templates/github/workflows/ci.yml new file mode 100644 index 0000000..0ec1aa6 --- /dev/null +++ b/generators/app/templates/github/workflows/ci.yml @@ -0,0 +1,21 @@ +name: CI +on: [push] +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [<%= nodeEngineVersion %>.x] + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }}<% if ( automatedTests && browserModule && !cloudBrowsers ) { %> + - run: export DISPLAY=:99.0; sh -e /etc/init.d/xvfb start; sleep 3;<% } %> + - run: npm install + - run: npm test<% if ( codeCoverageService ) { %> + posttest: + needs: test + steps: + - run: npm run posttest:ci<% } %> diff --git a/generators/app/templates/karma.conf.js b/generators/app/templates/karma.conf.js index be15ba0..f46de4c 100644 --- a/generators/app/templates/karma.conf.js +++ b/generators/app/templates/karma.conf.js @@ -16,7 +16,7 @@ process.env.CHROME_BIN = puppeteer.executablePath();<% } %> let config; const isCI = typeof process.env.CI !== 'undefined' && process.env.CI !== 'false'; -const isPR = typeof process.env.TRAVIS_PULL_REQUEST !== 'undefined' && process.env.TRAVIS_PULL_REQUEST !== 'false'; +const isPR = <% if ( ciService === 'travis' ) { %>typeof process.env.TRAVIS_PULL_REQUEST !== 'undefined' && process.env.TRAVIS_PULL_REQUEST !== 'false'<% } else { %>typeof process.env.GITHUB_HEAD_REF !== 'undefined' && process.env.GITHUB_HEAD_REF !== ''<% } %>; const local = !isCI || (isCI && isPR); const port = 0; diff --git a/test/index.js b/test/index.js index 149a0f8..f05ed81 100644 --- a/test/index.js +++ b/test/index.js @@ -201,6 +201,22 @@ describe('Automated tests', function () { }); }); +describe('Automated tests, different CI service', function () { + before(function () { + return helpers + .run(path.join(__dirname, '../generators/app')) + .withPrompts({ + automatedTests: true, + ciService: 'github' + }) + .toPromise(); + }); + + it('should create necessary files', function () { + assert.file(['.github/workflows/ci.yml']); + }); +}); + describe('Automated tests, browser module', function () { before(function () { return helpers