From 91869b318349b60869eead44b7798ea6ba056943 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Mon, 4 May 2020 12:23:06 +0200 Subject: [PATCH] standardization --- jenkins.js | 9 ++--- lib/snippets/jenkins/Assets.ts | 62 ++-------------------------------- lib/snippets/node/Assets.ts | 4 +-- lib/snippets/node/index.ts | 50 ++++++++++++++++++--------- package.json | 1 + yarn.lock | 5 +++ 6 files changed, 47 insertions(+), 84 deletions(-) diff --git a/jenkins.js b/jenkins.js index 59c1978..ec0a7aa 100644 --- a/jenkins.js +++ b/jenkins.js @@ -20,13 +20,8 @@ const pkg = JSON.parse (fs.readFileSync ('package.json', 'utf-8')); 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.execSync ('yarn test', { stdio: 'inherit' }); +child_process.execSync ('yarn compile', { stdio: 'inherit' }); child_process.exec ('git log -1 | grep \'\\[no publish\\]\'') .addListener ('exit', (code) => { diff --git a/lib/snippets/jenkins/Assets.ts b/lib/snippets/jenkins/Assets.ts index c7b4b9a..d07e567 100644 --- a/lib/snippets/jenkins/Assets.ts +++ b/lib/snippets/jenkins/Assets.ts @@ -1,7 +1,9 @@ /* eslint-disable max-len */ +import { files } from '@scode/standard'; + const general = { jenkinsfile: '' }; -const node = { jenkinsfile: '', js: '' }; +const node = { jenkinsfile: files.jenkinsfile, js: files.jenkins }; general.jenkinsfile = `pipeline { agent any @@ -44,62 +46,4 @@ general.jenkinsfile = `pipeline { } `; -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/node/Assets.ts b/lib/snippets/node/Assets.ts index 8be0e51..599f7c6 100644 --- a/lib/snippets/node/Assets.ts +++ b/lib/snippets/node/Assets.ts @@ -59,8 +59,8 @@ const tsconfig = `{ } `; -const eslintignore_ts = `/dist/ +const eslintignore = `/dist/ *.d.ts `; -export { eslintrc, gitignore, npmrc, tsconfig, eslintrc_ts, eslintignore_ts }; +export { eslintrc, gitignore, npmrc, tsconfig, eslintrc_ts, eslintignore }; diff --git a/lib/snippets/node/index.ts b/lib/snippets/node/index.ts index 454668f..07d7029 100644 --- a/lib/snippets/node/index.ts +++ b/lib/snippets/node/index.ts @@ -12,9 +12,37 @@ import { apply_template, modify_json, run_command } from '../../Helper'; import { eslintrc, gitignore, npmrc, - tsconfig, eslintrc_ts, eslintignore_ts + tsconfig, eslintrc_ts, eslintignore } from './Assets'; +/** + * initialize the package.json + * + * @param {string} folder folder + */ +async function init_package (folder: string): Promise { + await modify_json ((obj: Record) => { + const scripts + = { + lint: 'eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs', + test: 'nyc ava', + compile: 'tsc --allowJs --declaration --emitDeclarationOnly index.js' + }; + const files = [ 'LICENSE' ]; + if (use_ts) { + scripts.compile = 'tsc'; + scripts.test = 'tsc && nyc ava'; + files.push ('/dist/'); + } + else { + files.push ('*.js', '*.d.ts'); + } + obj.scripts = scripts; + obj.files = files; + return obj; + }, path.join (folder, 'package.json')); +} + export default class Node implements Snippet { public async start (): Promise { const folder = await new Input ( @@ -31,12 +59,12 @@ export default class Node implements Snippet { await apply_template (eslintrc, path.join (folder, '.eslintrc.js')); await apply_template (npmrc, path.join (folder, '.npmrc')); await apply_template (gitignore, path.join (folder, '.gitignore')); + await apply_template ( + eslintignore, + path.join (folder, '.eslintignore') + ); if (use_ts) { await apply_template (tsconfig, path.join (folder, 'tsconfig.json')); - await apply_template ( - eslintignore_ts, - path.join (folder, '.eslintignore') - ); await apply_template ( eslintrc_ts, path.join (folder, 'lib', '.eslintrc.js') @@ -51,16 +79,6 @@ export default class Node implements Snippet { folder ); - await modify_json ((obj: Record) => { - obj.scripts = { - lint: 'eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs', - test: 'nyc ava' - }; - if (use_ts) { - (obj.scripts as Record).compile = 'tsc'; - (obj.scripts as Record).test = 'tsc && nyc ava'; - } - return obj; - }, path.join (folder, 'package.json')); + await init_package (folder); } } diff --git a/package.json b/package.json index 30dc2b2..a296b9a 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "typescript": "^3.8.3" }, "dependencies": { + "@scode/standard": "^1.0.1", "enquirer": "^2.3.5", "fs-extra": "^9.0.0", "license": "^1.0.3" diff --git a/yarn.lock b/yarn.lock index b0994f1..ba2ab2a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -54,6 +54,11 @@ eslint-plugin-node "^11.0.0" eslint-plugin-sort-requires-by-path "^1.0.2" +"@scode/standard@^1.0.1": + version "1.0.3" + resolved "https://npm.scode.ovh/@scode%2fstandard/-/standard-1.0.3.tgz#39147b46e62e222555626302fdfb5f2986c5ffbe" + integrity sha512-cxeuAn8zmqTRa5i9sbQP74lRRdywl2bEg4Nl72rE+MOeAI06VzBPirxwSOiKr2ZUaFNGe/tTk1cIdiI4cYAIiQ== + "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"