complete other snippets
This commit is contained in:
parent
3c44614298
commit
f383ed0ce4
@ -1 +1,2 @@
|
||||
**/*.d.ts
|
||||
/dist/
|
||||
|
57
lib/Helper.ts
Normal file
57
lib/Helper.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import path from 'path';
|
||||
import child_process from 'child_process';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
/**
|
||||
* write a template to a file
|
||||
*
|
||||
* @param {string} contents file contents
|
||||
* @param {string} destination file destination
|
||||
*/
|
||||
async function apply_template (
|
||||
contents: string,
|
||||
destination: string
|
||||
): Promise<void> {
|
||||
const dst = path.join (process.cwd (), destination);
|
||||
if (!await fs.pathExists (dst))
|
||||
await fs.writeFile (dst, contents);
|
||||
}
|
||||
|
||||
type JSONMutator = {
|
||||
(json: object): Promise<object>;
|
||||
}
|
||||
|
||||
/**
|
||||
* modify a json file
|
||||
*
|
||||
* @param {Function} func function that modifies the object
|
||||
* @param {string} json_path path of json file
|
||||
*/
|
||||
async function modify_json (
|
||||
func: JSONMutator,
|
||||
json_path = 'package.json'
|
||||
): Promise<void> {
|
||||
const file_path = path.join (process.cwd (), json_path);
|
||||
const content = JSON.parse (await fs.readFile (file_path, 'utf-8'));
|
||||
const new_obj = await func (content);
|
||||
await fs.writeFile (file_path, JSON.stringify (new_obj, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* run a command
|
||||
*
|
||||
* @param {string} command command to run
|
||||
* @param {string} folder folder to run in
|
||||
* @returns {Promise<void>} promise
|
||||
*/
|
||||
function run_command (command: string, folder = ''): Promise<void> {
|
||||
return new Promise ((res) => {
|
||||
const exec = child_process.exec (
|
||||
command,
|
||||
{ cwd: path.join (process.cwd (), folder), stdio: 'inherit' }
|
||||
);
|
||||
exec.on ('close', res);
|
||||
});
|
||||
}
|
||||
|
||||
export { modify_json, apply_template, run_command };
|
@ -7,6 +7,7 @@
|
||||
|
||||
export class DialogHandler {
|
||||
public static catch (): void {
|
||||
// eslint-disable-next-line no-process-exit
|
||||
process.exit ();
|
||||
}
|
||||
}
|
||||
|
@ -19,42 +19,48 @@ import { CopyrightOptions } from './copyright_options';
|
||||
|
||||
export default class Copyright implements Snippet {
|
||||
private options: CopyrightOptions | null = null;
|
||||
private cwd: string = '';
|
||||
private loaded_from_config: boolean = false;
|
||||
private cwd = '';
|
||||
private loaded_from_config = false;
|
||||
|
||||
async start (cwd: string): Promise<void> {
|
||||
this.cwd = cwd;
|
||||
await this.load_options_file();
|
||||
await this.load_options_file ();
|
||||
if (!this.options)
|
||||
await this.gather_options ();
|
||||
|
||||
await FileMapper.map_all_files (
|
||||
this.cwd,
|
||||
this.fix_file_license.bind(this)
|
||||
this.fix_file_license.bind (this)
|
||||
);
|
||||
|
||||
if (!this.loaded_from_config && await new Confirm({
|
||||
message: 'should those settings be saved for the next run?'
|
||||
}).run().catch(DialogHandler.catch)) {
|
||||
this.save_options_file();
|
||||
}
|
||||
if (!this.loaded_from_config && await new Confirm (
|
||||
{ message: 'should those settings be saved for the next run?' }
|
||||
)
|
||||
.run ()
|
||||
.catch (DialogHandler.catch))
|
||||
this.save_options_file ();
|
||||
}
|
||||
|
||||
private async gather_options (): Promise<void> {
|
||||
this.options = (new CopyrightOptions);
|
||||
this.options.author = await new Input ({ message: 'author' })
|
||||
.run ().catch(DialogHandler.catch);
|
||||
.run ()
|
||||
.catch (DialogHandler.catch);
|
||||
this.options.email = await new Input ({ message: 'email' })
|
||||
.run ().catch(DialogHandler.catch);
|
||||
.run ()
|
||||
.catch (DialogHandler.catch);
|
||||
this.options.company = await new Input ({ message: 'company' })
|
||||
.run ().catch(DialogHandler.catch);
|
||||
.run ()
|
||||
.catch (DialogHandler.catch);
|
||||
this.options.software = await new Input ({ message: 'software name' })
|
||||
.run ().catch(DialogHandler.catch);
|
||||
.run ()
|
||||
.catch (DialogHandler.catch);
|
||||
this.options.has_license = await new Confirm ({
|
||||
message:
|
||||
'would you like to specify a license?'
|
||||
})
|
||||
.run ().catch(DialogHandler.catch);
|
||||
.run ()
|
||||
.catch (DialogHandler.catch);
|
||||
if (this.options.has_license) {
|
||||
this.options.license = await new AutoComplete ({
|
||||
name: 'license',
|
||||
@ -62,7 +68,8 @@ export default class Copyright implements Snippet {
|
||||
limit: 10,
|
||||
choices: findLicense ('')
|
||||
})
|
||||
.run ().catch(DialogHandler.catch);
|
||||
.run ()
|
||||
.catch (DialogHandler.catch);
|
||||
}
|
||||
}
|
||||
|
||||
@ -74,26 +81,32 @@ export default class Copyright implements Snippet {
|
||||
const options = JSON.parse (
|
||||
await fs.readFile (file_path, 'utf-8')
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`author: ${options.author}
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log (`author: ${options.author}
|
||||
email: ${options.email}
|
||||
company: ${options.company}
|
||||
software name: ${options.software}
|
||||
license: ${options.license}`);
|
||||
const should_load = await new Confirm({
|
||||
message: 'should those options be used?'
|
||||
}).run().catch(DialogHandler.catch);
|
||||
if (should_load){
|
||||
const should_load = await new Confirm (
|
||||
{ message: 'should those options be used?' }
|
||||
)
|
||||
.run ()
|
||||
.catch (DialogHandler.catch);
|
||||
if (should_load) {
|
||||
this.options = options;
|
||||
this.loaded_from_config = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async save_options_file(): Promise<void> {
|
||||
private async save_options_file (): Promise<void> {
|
||||
const file_path = path.join (this.cwd, '.liconfig.json');
|
||||
await fs.writeFile (file_path, JSON.stringify (this.options, null, 2), 'utf-8');
|
||||
await fs.writeFile (
|
||||
file_path,
|
||||
JSON.stringify (this.options, null, 2),
|
||||
'utf-8'
|
||||
);
|
||||
}
|
||||
|
||||
private fix_file_license (
|
||||
@ -107,7 +120,9 @@ license: ${options.license}`);
|
||||
if (!(/\.(?:js|ts|mjs)$/u).test (filename) && !regex.test (data))
|
||||
return null;
|
||||
return (shebang_line ? shebang_line[0] : '')
|
||||
+ CopyrightGenerator.get_copyright_notice (this.options as CopyrightOptions)
|
||||
+ CopyrightGenerator.get_copyright_notice (
|
||||
this.options as CopyrightOptions
|
||||
)
|
||||
+ data.replace (regex, '')
|
||||
.replace (shebang, '');
|
||||
}
|
||||
|
@ -1,228 +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 <timo@scode.ovh>, April 2020
|
||||
*/
|
||||
|
||||
/* eslint-disable no-magic-numbers */
|
||||
/* eslint-disable no-console */
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require ('fs-extra');
|
||||
|
||||
/**
|
||||
* convert short type notation to knex notation
|
||||
*
|
||||
* @param {string} short short type
|
||||
* @returns {string} type
|
||||
*/
|
||||
function get_type (short) {
|
||||
switch (short) {
|
||||
case '\'\'':
|
||||
return 'string';
|
||||
case '#':
|
||||
return 'integer';
|
||||
case '#.#':
|
||||
return 'double';
|
||||
case '✓':
|
||||
return 'boolean';
|
||||
case '🖹':
|
||||
return 'text';
|
||||
case '🕓':
|
||||
return 'timestamp';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns columns and attributes for a table
|
||||
*
|
||||
* @param {string} str table definition
|
||||
* @returns {object} table info
|
||||
*/
|
||||
function get_table_info (str) {
|
||||
const lines = str.split (/\n/ug);
|
||||
lines.splice (0, 2);
|
||||
lines.splice (lines.length - 2, 2);
|
||||
const name_line = lines.shift ();
|
||||
const { name } = (/<b>(?<name>\S+)<\/b>/u).exec (name_line).groups;
|
||||
const columns = [];
|
||||
while (lines.length > 0) {
|
||||
const col = {};
|
||||
const l = lines.shift ();
|
||||
const regex = /<tr><td.*?>(?<props>.*?)<\/td><\/tr>/u;
|
||||
const data = regex.exec (l).groups.props.split (/\s+/gu);
|
||||
if (data.length === 3 || data[0] === '🔑') {
|
||||
const opt = data.shift ();
|
||||
if (opt === '★')
|
||||
col.unique = true;
|
||||
if (opt === '🔑')
|
||||
col.type = 'increments';
|
||||
}
|
||||
|
||||
col.name = data.shift ();
|
||||
if (data.length > 0)
|
||||
col.type = get_type (data.shift ());
|
||||
|
||||
if (typeof col.type === 'undefined')
|
||||
console.error (`column type is undefined: ${col.name} table: ${name}`);
|
||||
|
||||
columns.push (col);
|
||||
}
|
||||
|
||||
return { name, columns, foreign_keys: [] };
|
||||
}
|
||||
|
||||
/**
|
||||
* get all tables from a structure file
|
||||
*
|
||||
* @param {string} file path to the structure file
|
||||
* @returns {Promise<object>} tables and foreign keys
|
||||
*/
|
||||
async function get_tables (file) {
|
||||
const lines = (await fs.readFile (file, 'utf-8')).split (/\n/gu);
|
||||
const curr = [];
|
||||
const tables = [];
|
||||
const foreign = [];
|
||||
for (const l of lines) {
|
||||
if (curr.length > 0 || (/\S+ \[label=</u).test (l))
|
||||
curr.push (l);
|
||||
|
||||
if ((/>.*?\]/u).test (l)) {
|
||||
const val = curr.join ('\n');
|
||||
if (val)
|
||||
tables.push (get_table_info (val));
|
||||
curr.splice (0, curr.length);
|
||||
}
|
||||
|
||||
get_foreign_key (l, foreign);
|
||||
}
|
||||
|
||||
for (const fk of foreign) {
|
||||
for (let i = 0; i < tables.length; i++) {
|
||||
if (tables[i].name === fk.table) {
|
||||
tables[i].foreign_keys.push (fk);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets foreign keys from a line
|
||||
*
|
||||
* @param {string} line line to check
|
||||
* @param {Array<object>} foreign_keys array to add to
|
||||
*/
|
||||
function get_foreign_key (line, foreign_keys) {
|
||||
const fk = (/(?<col>\S+) -> (?<ref>\S+)/u).exec (line);
|
||||
if (fk) {
|
||||
const col = fk.groups.col.split (':');
|
||||
const ref = fk.groups.ref.split (':');
|
||||
|
||||
const foreign_key = {
|
||||
table: col[0],
|
||||
column: col[1],
|
||||
ref_table: ref[0],
|
||||
ref_column: ref[1]
|
||||
};
|
||||
|
||||
foreign_keys.push (foreign_key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a function for creating a table
|
||||
*
|
||||
* @param {object} table table to create a function for
|
||||
* @returns {string} function
|
||||
*/
|
||||
function create_table_function (table) {
|
||||
let func = `/**
|
||||
* create table ${table.name}
|
||||
*
|
||||
* @param {any} knex database connection
|
||||
* @returns {Promise} result
|
||||
*/
|
||||
function create_${table.name} (knex) {
|
||||
return knex.schema.createTable ('${table.name}', (table) => {
|
||||
${table.columns
|
||||
.map ((col) => `table.${col.type} ('${col.name}');`)
|
||||
.join ('\n ')}
|
||||
`;
|
||||
|
||||
const unique = table.columns.filter ((val) => val.unique);
|
||||
if (unique.length > 0) {
|
||||
func += `\n table.unique (${unique
|
||||
.map ((val) => `'${val.name}'`)
|
||||
.join (', ')});\n`;
|
||||
}
|
||||
|
||||
if (table.foreign_keys.length > 0) {
|
||||
func += '\n';
|
||||
for (const fk of table.foreign_keys) {
|
||||
func += ` table.foreign ('${fk.column}')
|
||||
.references ('${fk.ref_column}')
|
||||
.inTable ('${fk.ref_table}');\n`;
|
||||
}
|
||||
}
|
||||
|
||||
func += ` });
|
||||
}`;
|
||||
|
||||
return func;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates the migration function
|
||||
*
|
||||
* @param {Array<object>} tables table array
|
||||
* @returns {string} function
|
||||
*/
|
||||
function create_up_function (tables) {
|
||||
const func = `async function up (knex) {
|
||||
${tables.map ((val) => ` await create_${val.name} (knex);`)
|
||||
.join ('\n')}
|
||||
}`;
|
||||
return func;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates the complete migration file for a graph
|
||||
*
|
||||
* @param {string} file_path file to scan
|
||||
* @returns {Promise<string>} file
|
||||
*/
|
||||
async function create_migration (file_path) {
|
||||
const tables = await get_tables (file_path);
|
||||
const functions = tables.map ((tab) => create_table_function (tab));
|
||||
|
||||
const file = `'use strict';
|
||||
|
||||
${functions.join ('\n\n')}
|
||||
|
||||
/**
|
||||
* run migration
|
||||
*
|
||||
* @param {any} knex db connection
|
||||
*/
|
||||
${create_up_function (tables)}
|
||||
|
||||
/**
|
||||
* revert migration
|
||||
*/
|
||||
function down () {
|
||||
// noop
|
||||
}
|
||||
|
||||
module.exports = { up, down };
|
||||
`;
|
||||
return file;
|
||||
}
|
||||
|
||||
module.exports = { get_tables, create_table_function, create_migration };
|
@ -1,66 +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 <timo@scode.ovh>, April 2020
|
||||
*/
|
||||
|
||||
/* eslint-disable no-magic-numbers */
|
||||
/* eslint-disable no-sync */
|
||||
/* eslint-disable no-console */
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require ('fs-extra');
|
||||
const path = require ('path');
|
||||
const dot_parser = require ('./dot_parser');
|
||||
|
||||
/**
|
||||
* copies the full template to a new folder named after arg[0]
|
||||
*
|
||||
* @param {string} folder folder to run in
|
||||
* @param {Array} args function arguments
|
||||
*/
|
||||
async function run (folder, args) {
|
||||
const graph = path.join (folder, args[0]);
|
||||
const migration = path.join (folder, args[1]);
|
||||
|
||||
const db_migration = await dot_parser.create_migration (graph);
|
||||
await fs.writeFile (migration, db_migration, 'utf-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if the arguments meet the requirements
|
||||
*
|
||||
* @param {string} folder folder to run in
|
||||
* @param {Array} args function arguments
|
||||
* @returns {boolean} true if arguments match requirements
|
||||
*/
|
||||
function assert (folder, args) {
|
||||
const tests = [
|
||||
{
|
||||
f: () => (args.length === 2
|
||||
&& typeof args[0] === 'string'
|
||||
&& typeof args[1] === 'string'),
|
||||
reason: 'db [graph] [migration]'
|
||||
},
|
||||
{
|
||||
f: () => (typeof folder === 'string'),
|
||||
reason: 'cwd is not a folder (internal error)'
|
||||
},
|
||||
{
|
||||
f: () => (fs.existsSync (folder)),
|
||||
reason: 'cwd does not exist (internal error)'
|
||||
}
|
||||
];
|
||||
for (const test of tests) {
|
||||
if (!test.f ()) {
|
||||
console.log (test.reason);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = { run, assert };
|
@ -1,78 +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 <timo@scode.ovh>, April 2020
|
||||
*/
|
||||
|
||||
/* eslint-disable no-sync */
|
||||
/* eslint-disable no-console */
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require ('fs-extra');
|
||||
const path = require ('path');
|
||||
|
||||
/**
|
||||
* copies the full template to a new folder named after arg[0]
|
||||
*
|
||||
* @param {string} folder folder to run in
|
||||
* @param {Array} args function arguments
|
||||
*/
|
||||
function run (folder, args) {
|
||||
const is_node = args.length === 1 && (/^node$/ui).test (args[0]);
|
||||
|
||||
const template = path.join (
|
||||
__dirname,
|
||||
'template',
|
||||
is_node
|
||||
? 'node'
|
||||
: 'general'
|
||||
);
|
||||
|
||||
for (const f of fs.readdirSync (template)) {
|
||||
fs.copy (
|
||||
path.join (template, f),
|
||||
path.join (folder, f),
|
||||
{ filter: (src, dest) => !fs.existsSync (dest) }
|
||||
);
|
||||
}
|
||||
|
||||
if (is_node) {
|
||||
const pkg = path.join (folder, 'package.json');
|
||||
if (fs.existsSync (pkg)) {
|
||||
const json = JSON.parse (fs.readFileSync (pkg, 'utf-8'));
|
||||
json.scripts.ci = 'yarn && node jenkins.js';
|
||||
fs.writeFileSync (pkg, JSON.stringify (json, null, 2), 'utf-8');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if the arguments meet the requirements
|
||||
*
|
||||
* @param {string} folder folder to run in
|
||||
* @returns {boolean} true if arguments match requirements
|
||||
*/
|
||||
function assert (folder) {
|
||||
const tests = [
|
||||
{
|
||||
f: () => (typeof folder === 'string'),
|
||||
reason: 'cwd is not a folder (internal error)'
|
||||
},
|
||||
{
|
||||
f: () => (fs.existsSync (folder)),
|
||||
reason: 'cwd does not exist (internal error)'
|
||||
}
|
||||
];
|
||||
for (const test of tests) {
|
||||
if (!test.f ()) {
|
||||
console.log (test.reason);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = { run, assert };
|
36
lib/snippets/jenkins/index.ts
Normal file
36
lib/snippets/jenkins/index.ts
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 <timo@scode.ovh>, April 2020
|
||||
*/
|
||||
|
||||
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';
|
||||
|
||||
export class Jenkins implements Snippet {
|
||||
public async start (): Promise<void> {
|
||||
const is_node = await new Confirm ({
|
||||
message: 'is the current project using nodejs?',
|
||||
initial: true
|
||||
})
|
||||
.run ();
|
||||
|
||||
if (is_node) {
|
||||
await apply_template (node_js, 'jenkins.js');
|
||||
await apply_template (node_jenkinsfile, 'Jenkinsfile');
|
||||
await modify_json ((obj) => {
|
||||
obj.scripts.ci = 'yarn && node jenkins.js';
|
||||
return obj;
|
||||
});
|
||||
}
|
||||
else {
|
||||
await apply_template (general_jenkinsfile, 'Jenkinsfile');
|
||||
}
|
||||
}
|
||||
}
|
@ -22,6 +22,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.exec ('git log -1 | grep \'\\[no publish\\]\'')
|
||||
.addListener ('exit', (code) => {
|
@ -1,127 +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 <timo@scode.ovh>, April 2020
|
||||
*/
|
||||
|
||||
/* eslint-disable no-sync */
|
||||
/* eslint-disable no-console */
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require ('fs-extra');
|
||||
const path = require ('path');
|
||||
const child_process = require ('child_process');
|
||||
|
||||
/**
|
||||
* copies the full template to a new folder named after arg[0]
|
||||
*
|
||||
* @param {string} folder folder to run in
|
||||
* @param {Array} args function arguments
|
||||
*/
|
||||
async function run (folder, args) {
|
||||
const snip_folder_path = [ folder ];
|
||||
if (args.length > 0)
|
||||
snip_folder_path.push (args[0]);
|
||||
const snip_folder = path.join (...snip_folder_path);
|
||||
const template = path.join (__dirname, 'template');
|
||||
if (!fs.existsSync (snip_folder))
|
||||
fs.mkdir (snip_folder);
|
||||
for (const f of fs.readdirSync (template)) {
|
||||
fs.copy (
|
||||
path.join (template, f),
|
||||
path.join (snip_folder, f),
|
||||
{
|
||||
recursive: true,
|
||||
filter: (src, dest) => !fs.existsSync (dest)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
child_process.execSync (
|
||||
'git init',
|
||||
{ cwd: snip_folder, stdio: 'inherit' }
|
||||
);
|
||||
child_process.execSync (
|
||||
'yarn init -y',
|
||||
{ cwd: snip_folder, stdio: 'inherit' }
|
||||
);
|
||||
child_process.execSync (
|
||||
'yarn add --dev @scode/eslint-config eslint nyc ava',
|
||||
{ cwd: snip_folder, stdio: 'inherit' }
|
||||
);
|
||||
|
||||
const package_json = JSON.parse (
|
||||
await fs.readFile (path.join (snip_folder, 'package.json'), 'utf-8')
|
||||
);
|
||||
|
||||
package_json.scripts = {
|
||||
lint: 'eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs',
|
||||
test: 'nyc ava'
|
||||
};
|
||||
|
||||
await fs.writeFile (
|
||||
path.join (snip_folder, 'package.json'),
|
||||
JSON.stringify (package_json, null, 2)
|
||||
);
|
||||
|
||||
await fs.writeFile (
|
||||
path.join (snip_folder, '.gitignore'),
|
||||
`/node_modules/
|
||||
/dist/
|
||||
/.nyc_output/
|
||||
/coverage/`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if the arguments meet the requirements
|
||||
*
|
||||
* @param {string} folder folder to run in
|
||||
* @param {Array} args function arguments
|
||||
* @returns {boolean} true if arguments match requirements
|
||||
*/
|
||||
function assert (folder, args) {
|
||||
const tests = [
|
||||
{
|
||||
f: () => (args.length < 2),
|
||||
reason: 'too many arguments'
|
||||
},
|
||||
{
|
||||
f: () => (args.length === 0 || typeof args[0] === 'string'),
|
||||
reason: 'name is not a string'
|
||||
},
|
||||
{
|
||||
f: () => (args.length === 0 || (/^[a-z-]+$/iu).test (args[0])),
|
||||
reason: 'name can only contain [a-z-]'
|
||||
},
|
||||
{
|
||||
f: () => (typeof folder === 'string'),
|
||||
reason: 'cwd is not a folder (internal error)'
|
||||
},
|
||||
{
|
||||
f: () => (fs.existsSync (folder)),
|
||||
reason: 'cwd does not exist (internal error)'
|
||||
},
|
||||
{
|
||||
f: () => (args.length === 1 || fs.readdirSync (folder).length === 0),
|
||||
reason: 'folder is not empty'
|
||||
},
|
||||
{
|
||||
f: () => (args.length === 0
|
||||
|| !fs.existsSync (path.join (folder, args[0]))),
|
||||
reason: 'folder already exists'
|
||||
}
|
||||
];
|
||||
for (const test of tests) {
|
||||
if (!test.f ()) {
|
||||
console.log (test.reason);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = { run, assert };
|
46
lib/snippets/node/index.ts
Normal file
46
lib/snippets/node/index.ts
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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 <timo@scode.ovh>, April 2020
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import { Input } from 'enquirer';
|
||||
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';
|
||||
|
||||
export class Node implements Snippet {
|
||||
public async start (): Promise<void> {
|
||||
const folder = await new Input (
|
||||
{ message: 'project name (leave empty for current folder):' }
|
||||
)
|
||||
.run ();
|
||||
|
||||
await apply_template (eslintrc, path.join (folder, '.eslintrc.js'));
|
||||
await apply_template (npmrc, path.join (folder, '.npmrc'));
|
||||
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 (
|
||||
'yarn add --dev @scode/eslint-config eslint nyc ava',
|
||||
folder
|
||||
);
|
||||
|
||||
await modify_json ((obj) => {
|
||||
obj.scripts = {
|
||||
lint: 'eslint . --ext .js,.jsx,.ts,.tsx,.vue,.mjs',
|
||||
test: 'nyc ava'
|
||||
};
|
||||
return obj;
|
||||
}, path.join (folder, 'package.json'));
|
||||
}
|
||||
}
|
4
lib/snippets/node/template/gitignore.asset
Normal file
4
lib/snippets/node/template/gitignore.asset
Normal file
@ -0,0 +1,4 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
/.nyc_output/
|
||||
/coverage/
|
52
yarn.lock
52
yarn.lock
@ -29,17 +29,17 @@
|
||||
integrity sha512-IHjc31WXciQT3hfvdY+M59jBkQp70Fpr04tNDVO5rez2PNv4u8tE6w//CkU+GeBoO9k2ahneSqzjzvlgjyjkGw==
|
||||
|
||||
"@scode/eslint-config-es6@^1.0.1":
|
||||
version "1.0.20"
|
||||
resolved "https://npm.scode.ovh/@scode%2feslint-config-es6/-/eslint-config-es6-1.0.20.tgz#b4a5deeab8168a73233b41149134d85bb075638f"
|
||||
integrity sha512-h00GK6F5jycy7OmmXa9QHHIbv1x7DmMBuwLJSpl+lJD+pi7AUCCQv7oHmZ8autCZXXC9/8bPwJWrH9udIuiujg==
|
||||
version "1.0.21"
|
||||
resolved "https://npm.scode.ovh/@scode%2feslint-config-es6/-/eslint-config-es6-1.0.21.tgz#280bed045915dfa40070c465a12e5636f98658a2"
|
||||
integrity sha512-H0XyYtKpq6oZvVdr/+r2CwDcSK8qSVGuCwJzAlD2YXGnQH6QVl5XPav9cvYdMKg06W7ufGP9k1L3JseJMM0H5g==
|
||||
dependencies:
|
||||
"@scode/eslint-config" "^2.0.1"
|
||||
eslint-plugin-import "^2.20.1"
|
||||
|
||||
"@scode/eslint-config-ts@^1.0.19":
|
||||
version "1.0.19"
|
||||
resolved "https://npm.scode.ovh/@scode%2feslint-config-ts/-/eslint-config-ts-1.0.19.tgz#266e35a1c906aaa15c29a4ed29d85c76b2a02093"
|
||||
integrity sha512-SCYK6b2iOhtEfHjLfghWz1GJ7QNd8w2vLBNLWVG3s0u3iBVo6ppA+OCjAhEylJihigTJTwgv0hqvTDgn7jG4Tw==
|
||||
version "1.0.21"
|
||||
resolved "https://npm.scode.ovh/@scode%2feslint-config-ts/-/eslint-config-ts-1.0.21.tgz#19a87b5db5d7a89aaad00abf543fb1d51c20caf5"
|
||||
integrity sha512-rKDaF54iLt46tLwazDE2WcHvFtOHxhPvnj+ZepZl5ZSP6CC6aS6G97OUV1kfKryb1VZe+QDumw6yMmkXtt4mzg==
|
||||
dependencies:
|
||||
"@scode/eslint-config-es6" "^1.0.1"
|
||||
"@typescript-eslint/eslint-plugin" "^2.26.0"
|
||||
@ -77,9 +77,9 @@
|
||||
integrity sha512-8+KAKzEvSUdeo+kmqnKrqgeE+LcA0tjYWFY7RPProVYwnqDjukzO+3b6dLD56rYX5TdWejnEOLJYOIeh4CXKuA==
|
||||
|
||||
"@types/node@*", "@types/node@^13.11.1":
|
||||
version "13.11.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.11.1.tgz#49a2a83df9d26daacead30d0ccc8762b128d53c7"
|
||||
integrity sha512-eWQGP3qtxwL8FGneRrC5DwrJLGN4/dH1clNTuLfN81HCrxVtxRjygDTUoZJ5ASlDEeo0ppYFQjQIlXhtXpOn6g==
|
||||
version "13.13.0"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.13.0.tgz#30d2d09f623fe32cde9cb582c7a6eda2788ce4a8"
|
||||
integrity sha512-WE4IOAC6r/yBZss1oQGM5zs2D7RuKR6Q+w+X2SouPofnWn+LbCqClRyhO3ZE7Ix8nmFgo/oVuuE01cJT2XB13A==
|
||||
|
||||
"@typescript-eslint/eslint-plugin@^2.26.0":
|
||||
version "2.28.0"
|
||||
@ -268,9 +268,9 @@ cli-cursor@^3.1.0:
|
||||
restore-cursor "^3.1.0"
|
||||
|
||||
cli-width@^2.0.0:
|
||||
version "2.2.0"
|
||||
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639"
|
||||
integrity sha1-/xnt6Kml5XkyQUewwR8PvLq+1jk=
|
||||
version "2.2.1"
|
||||
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.1.tgz#b0433d0b4e9c847ef18868a4ef16fd5fc8271c48"
|
||||
integrity sha512-GRMWDxpOB6Dgk2E5Uo+3eEBvtOOlimMmpbFiKuLFnQzYDavtLFY3K5ona41jgN/WdRZtG7utuVSVTL4HbZHGkw==
|
||||
|
||||
cliui@^6.0.0:
|
||||
version "6.0.0"
|
||||
@ -618,11 +618,11 @@ esprima@^4.0.0:
|
||||
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
|
||||
|
||||
esquery@^1.0.1:
|
||||
version "1.2.1"
|
||||
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.2.1.tgz#105239e215c5aa480369c7794d74b8b5914c19d4"
|
||||
integrity sha512-/IcAXa9GWOX9BUIb/Tz2QrrAWFWzWGrFIeLeMRwtiuwg9qTFhSYemsi9DixwrFFqVbhBZ47vGcxEnu5mbPqbig==
|
||||
version "1.3.1"
|
||||
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.3.1.tgz#b78b5828aa8e214e29fb74c4d5b752e1c033da57"
|
||||
integrity sha512-olpvt9QG0vniUBZspVRN6lwB7hOZoTRtT+jzR+tS4ffYx2mzbw+z0XCOk44aaLYKApNX5nMm+E+P6o25ip/DHQ==
|
||||
dependencies:
|
||||
estraverse "^5.0.0"
|
||||
estraverse "^5.1.0"
|
||||
|
||||
esrecurse@^4.1.0:
|
||||
version "4.2.1"
|
||||
@ -636,10 +636,10 @@ estraverse@^4.1.0, estraverse@^4.1.1:
|
||||
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.3.0.tgz#398ad3f3c5a24948be7725e83d11a7de28cdbd1d"
|
||||
integrity sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw==
|
||||
|
||||
estraverse@^5.0.0:
|
||||
version "5.0.0"
|
||||
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.0.0.tgz#ac81750b482c11cca26e4b07e83ed8f75fbcdc22"
|
||||
integrity sha512-j3acdrMzqrxmJTNj5dbr1YbjacrYgAxVMeF0gK16E3j494mOe7xygM/ZLIguEQ0ETwAg2hlJCtHRGav+y0Ny5A==
|
||||
estraverse@^5.1.0:
|
||||
version "5.1.0"
|
||||
resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-5.1.0.tgz#374309d39fd935ae500e7b92e8a6b4c720e59642"
|
||||
integrity sha512-FyohXK+R0vE+y1nHLoBM7ZTyqRpqAlhdZHCWIWEviFLiGB8b04H6bQs8G+XTthacvT8VuwvteiP7RJSxMs8UEw==
|
||||
|
||||
esutils@^2.0.2:
|
||||
version "2.0.3"
|
||||
@ -1356,9 +1356,9 @@ resolve-from@^4.0.0:
|
||||
integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==
|
||||
|
||||
resolve@^1.10.0, resolve@^1.10.1, resolve@^1.12.0, resolve@^1.13.1:
|
||||
version "1.15.1"
|
||||
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.15.1.tgz#27bdcdeffeaf2d6244b95bb0f9f4b4653451f3e8"
|
||||
integrity sha512-84oo6ZTtoTUpjgNEr5SJyzQhzL72gaRodsSfyxC/AXRvwu0Yse9H8eF9IpGo7b8YetZhlI6v7ZQ6bKBFV/6S7w==
|
||||
version "1.16.1"
|
||||
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.16.1.tgz#49fac5d8bacf1fd53f200fa51247ae736175832c"
|
||||
integrity sha512-rmAglCSqWWMrrBv/XM6sW0NuRFiKViw/W4d9EbC4pt+49H8JwHy+mcGmALTEg504AUDcLTvb1T2q3E9AnmY+ig==
|
||||
dependencies:
|
||||
path-parse "^1.0.6"
|
||||
|
||||
@ -1727,9 +1727,9 @@ y18n@^4.0.0:
|
||||
integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==
|
||||
|
||||
yargs-parser@^18.1.1:
|
||||
version "18.1.2"
|
||||
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.2.tgz#2f482bea2136dbde0861683abea7756d30b504f1"
|
||||
integrity sha512-hlIPNR3IzC1YuL1c2UwwDKpXlNFBqD1Fswwh1khz5+d8Cq/8yc/Mn0i+rQXduu8hcrFKvO7Eryk+09NecTQAAQ==
|
||||
version "18.1.3"
|
||||
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0"
|
||||
integrity sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==
|
||||
dependencies:
|
||||
camelcase "^5.0.0"
|
||||
decamelize "^1.2.0"
|
||||
|
Loading…
x
Reference in New Issue
Block a user