diff --git a/lib/snippets/jenkins/template/general/Jenkinsfile.asset b/assets/jenkins/general/Jenkinsfile.asset similarity index 100% rename from lib/snippets/jenkins/template/general/Jenkinsfile.asset rename to assets/jenkins/general/Jenkinsfile.asset diff --git a/lib/snippets/jenkins/template/node/Jenkinsfile.asset b/assets/jenkins/node/Jenkinsfile.asset similarity index 100% rename from lib/snippets/jenkins/template/node/Jenkinsfile.asset rename to assets/jenkins/node/Jenkinsfile.asset diff --git a/lib/snippets/jenkins/template/node/jenkins.js.asset b/assets/jenkins/node/jenkins.js.asset similarity index 100% rename from lib/snippets/jenkins/template/node/jenkins.js.asset rename to assets/jenkins/node/jenkins.js.asset diff --git a/lib/Helper.ts b/lib/Helper.ts index ef22ceb..66729a2 100644 --- a/lib/Helper.ts +++ b/lib/Helper.ts @@ -13,8 +13,10 @@ async function apply_template ( destination: string ): Promise { const dst = path.join (process.cwd (), destination); - if (!await fs.pathExists (dst)) + if (!await fs.pathExists (dst)) { + await fs.mkdirp (path.dirname (dst)); await fs.writeFile (dst, contents); + } } type JSONMutator = { @@ -43,22 +45,13 @@ async function modify_json ( * * @param {string} command command to run * @param {string} folder folder to run in - * @returns {Promise} promise */ -function run_command (command: string, folder = ''): Promise { - return new Promise ((res) => { - child_process.exec ( - command, - { cwd: path.join (process.cwd (), folder) }, - (err, stdout, stderr) => { - if (err) - throw err; - // eslint-disable-next-line no-console - console.log (stdout + stderr); - res (); - } - ); - }); +function run_command (command: string, folder = ''): void { + // eslint-disable-next-line no-sync + child_process.execSync ( + command, + { cwd: path.join (process.cwd (), folder), stdio: 'inherit' } + ); } export { modify_json, apply_template, run_command }; diff --git a/lib/asset.d.ts b/lib/asset.d.ts deleted file mode 100644 index df3ef2d..0000000 --- a/lib/asset.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -/* - * Copyright (C) SapphireCode - All Rights Reserved - * This file is part of Snippeteer which is released under BSD-3-Clause. - * See file 'LICENSE' for full license details. - * Created by Timo Hocker , April 2020 - */ - -declare module '*.asset' { - const content: any; - export default content; -} diff --git a/lib/snippets/jenkins/Assets.ts b/lib/snippets/jenkins/Assets.ts new file mode 100644 index 0000000..879dd8d --- /dev/null +++ b/lib/snippets/jenkins/Assets.ts @@ -0,0 +1,102 @@ +/* eslint-disable max-len */ + +const general = { jenkinsfile: '' }; +const node = { jenkinsfile: '', js: '' }; + +general.jenkinsfile = `pipeline { + agent any + + environment { + VERSION = VersionNumber([ + versionNumberString: + '\${BUILDS_ALL_TIME}', + versionPrefix: '1.0.', + worstResultForIncrement: 'SUCCESS' + ]) + publish = 0 + } + + stages { + stage('Setup') { + steps { + script { + currentBuild.displayName = env.VERSION + } + echo 'Setting up test environment' + sh 'echo setup' + } + } + } + post { + success { + script { + publish = sh script: "git log -1 | grep '\\\\[no publish\\\\]'", returnStatus: true + if (publish != 0) { + echo 'Deploying' + sh 'echo deploy' + } else { + echo 'Build successful, Commit not marked for deploying' + currentBuild.result = "UNSTABLE" + } + } + } + } +} +`; + +node.jenkinsfile = `pipeline { + agent any + + environment { + VERSION = VersionNumber([ + versionNumberString: + '\${BUILDS_ALL_TIME}', + versionPrefix: '1.0.', + worstResultForIncrement: 'SUCCESS' + ]) + } + + stages { + stage('Building') { + steps { + script { + currentBuild.displayName = env.VERSION + } + sh 'yarn ci \${VERSION}' + } + } + } +} +`; + +node.js = `/* eslint-disable no-process-exit */ +/* eslint-disable no-console */ +/* eslint-disable no-sync */ +'use strict'; + +const fs = require ('fs'); +const child_process = require ('child_process'); + +const pkg = JSON.parse (fs.readFileSync ('package.json', 'utf-8')); +[ + ,, pkg.version +] = process.argv; +fs.writeFileSync ('package.json', JSON.stringify (pkg, null, 2)); + +child_process.execSync ('yarn lint', { stdio: 'inherit' }); +if (typeof pkg.scripts !== 'undefined' && typeof pkg.scripts.test === 'string') + child_process.execSync ('yarn test', { stdio: 'inherit' }); +if (typeof pkg.scripts !== 'undefined' && typeof pkg.scripts.compile === 'string') + child_process.execSync ('yarn compile', { stdio: 'inherit' }); + +child_process.exec ('git log -1 | grep \\'\\\\[no publish\\\\]\\'') + .addListener ('exit', (code) => { + if (code === 0) { + console.log ('build not marked for deployment'); + process.exit (1); + } + else { child_process.execSync ('yarn publish'); } + }); +`; + +export { general, node }; diff --git a/lib/snippets/jenkins/index.ts b/lib/snippets/jenkins/index.ts index 3e19f5d..199b90d 100644 --- a/lib/snippets/jenkins/index.ts +++ b/lib/snippets/jenkins/index.ts @@ -9,11 +9,9 @@ import { Confirm } from 'enquirer'; import { Snippet } from '../../Snippet'; import { apply_template, modify_json } from '../../Helper'; -import node_js from './template/node/jenkins.js.asset'; -import node_jenkinsfile from './template/node/Jenkinsfile.asset'; -import general_jenkinsfile from './template/general/Jenkinsfile.asset'; +import { general, node } from './Assets'; -export class Jenkins implements Snippet { +export default class Jenkins implements Snippet { public async start (): Promise { const is_node = await new Confirm ({ message: 'is the current project using nodejs?', @@ -22,8 +20,8 @@ export class Jenkins implements Snippet { .run (); if (is_node) { - await apply_template (node_js, 'jenkins.js'); - await apply_template (node_jenkinsfile, 'Jenkinsfile'); + await apply_template (node.js, 'jenkins.js'); + await apply_template (node.jenkinsfile, 'Jenkinsfile'); // eslint-disable-next-line @typescript-eslint/no-explicit-any await modify_json ((obj: any): any => { obj.scripts.ci = 'yarn && node jenkins.js'; @@ -31,7 +29,7 @@ export class Jenkins implements Snippet { }); } else { - await apply_template (general_jenkinsfile, 'Jenkinsfile'); + await apply_template (general.jenkinsfile, 'Jenkinsfile'); } } } diff --git a/lib/snippets/node/Assets.ts b/lib/snippets/node/Assets.ts new file mode 100644 index 0000000..7645528 --- /dev/null +++ b/lib/snippets/node/Assets.ts @@ -0,0 +1,31 @@ +const eslintrc = `module.exports = { + env: { + commonjs: true, + es6: true, + node: true + }, + extends: [ + '@scode' + ], + globals: { + Atomics: 'readonly', + SharedArrayBuffer: 'readonly' + }, + parserOptions: { + ecmaVersion: 2018 + } +} +`; + +const gitignore = `/node_modules/ +/dist/ +/.nyc_output/ +/coverage/ +`; + +const index = ''; + +const npmrc = `@scode:registry=https://npm.scode.ovh +`; + +export { eslintrc, gitignore, index, npmrc }; diff --git a/lib/snippets/node/index.ts b/lib/snippets/node/index.ts index f72115c..01a5d16 100644 --- a/lib/snippets/node/index.ts +++ b/lib/snippets/node/index.ts @@ -11,12 +11,9 @@ import { Snippet } from '../../Snippet'; import { apply_template, modify_json, run_command } from '../../Helper'; -import eslintrc from './template/eslintrc.asset'; -import npmrc from './template/npmrc.asset'; -import index from './template/index.asset'; -import gitignore from './template/gitignore.asset'; +import { eslintrc, gitignore, index, npmrc } from './Assets'; -export class Node implements Snippet { +export default class Node implements Snippet { public async start (): Promise { const folder = await new Input ( { message: 'project name (leave empty for current folder):' } @@ -28,9 +25,9 @@ export class Node implements Snippet { await apply_template (index, path.join (folder, 'index.js')); await apply_template (gitignore, path.join (folder, '.gitignore')); - await run_command ('git init', folder); - await run_command ('yarn init -y', folder); - await run_command ( + run_command ('git init', folder); + run_command ('yarn init -y', folder); + run_command ( 'yarn add --dev @scode/eslint-config eslint nyc ava', folder ); diff --git a/lib/snippets/node/template/eslintrc.asset b/lib/snippets/node/template/eslintrc.asset deleted file mode 100644 index 6710f8a..0000000 --- a/lib/snippets/node/template/eslintrc.asset +++ /dev/null @@ -1,17 +0,0 @@ -module.exports = { - env: { - commonjs: true, - es6: true, - node: true - }, - extends: [ - '@scode' - ], - globals: { - Atomics: 'readonly', - SharedArrayBuffer: 'readonly' - }, - parserOptions: { - ecmaVersion: 2018 - } -} diff --git a/lib/snippets/node/template/gitignore.asset b/lib/snippets/node/template/gitignore.asset deleted file mode 100644 index e5c296b..0000000 --- a/lib/snippets/node/template/gitignore.asset +++ /dev/null @@ -1,4 +0,0 @@ -/node_modules/ -/dist/ -/.nyc_output/ -/coverage/ diff --git a/lib/snippets/node/template/index.asset b/lib/snippets/node/template/index.asset deleted file mode 100644 index e69de29..0000000 diff --git a/lib/snippets/node/template/npmrc.asset b/lib/snippets/node/template/npmrc.asset deleted file mode 100644 index 037041f..0000000 --- a/lib/snippets/node/template/npmrc.asset +++ /dev/null @@ -1 +0,0 @@ -@scode:registry=https://npm.scode.ovh diff --git a/package.json b/package.json index e03685f..102a62b 100644 --- a/package.json +++ b/package.json @@ -16,15 +16,16 @@ "url": "git@git.scode.ovh:timo/snippeteer" }, "files": [ - "/snippets/", + "/dist/", + "/assets/", "LICENSE" ], "author": "Timo Hocker", "license": "BSD-3-Clause", "devDependencies": { - "@scode/eslint-config-ts": "^1.0.19", + "@scode/eslint-config-ts": "^1.0.21", "@types/fs-extra": "^8.1.0", - "@types/node": "^13.11.1", + "@types/node": "^13.13.0", "eslint": "^6.8.0", "typescript": "^3.8.3" },