typescript, interactive

This commit is contained in:
2020-04-15 20:21:00 +02:00
parent 93862eff16
commit bc06152359
36 changed files with 2730 additions and 654 deletions

View File

@ -0,0 +1,34 @@
import { CopyrightOptions } from './copyright_options';
export class CopyrightGenerator {
public static get_copyright_notice (
opt: CopyrightOptions
): string {
let notice = '';
const date = (new Date);
const dtf = new Intl.DateTimeFormat ('en', { month: 'long' });
const year = date.getFullYear ();
const month = dtf.format (date);
if (opt.has_license) {
notice = `${'/*'}
* Copyright (C) ${opt.company || opt.author} - All Rights Reserved
* This file is part of ${opt.software} which is released under ${
opt.license}.
* See file 'LICENSE' for full license details.
* Created by ${opt.author} <${opt.email}>, ${month} ${year}
*/
`;
}
else {
notice = `${'/*'}
* Copyright (C) ${opt.company || opt.author} - All Rights Reserved
* Created by ${opt.author} <${opt.email}>, ${month} ${year}
*/
`;
}
return notice;
}
}

View File

@ -0,0 +1,8 @@
export class CopyrightOptions {
public has_license = false;
public license = '';
public author = '';
public company = '';
public email = '';
public software = '';
}

View File

@ -0,0 +1,27 @@
/* eslint-disable no-await-in-loop */
import path from 'path';
import fs from 'fs-extra';
export class FileMapper {
public static async map_all_files (
folder: string,
mutator: Function,
args: Array<unknown> = []
): Promise<void> {
const files = await fs.readdir (folder);
for (const file of files) {
if ([ 'node_modules' ].includes (file))
continue;
const abs_path = path.join (folder, file);
if ((await fs.stat (abs_path)).isDirectory ()) {
await FileMapper.map_all_files (abs_path, mutator, args);
continue;
}
const data = await fs.readFile (abs_path, 'utf-8');
const res = mutator (data, file, args);
if (res === null)
continue;
await fs.writeFile (abs_path, res, 'utf-8');
}
}
}

View File

@ -0,0 +1,87 @@
/* eslint-disable no-await-in-loop */
import path from 'path';
import fs from 'fs-extra';
import { Confirm, Input, AutoComplete } from 'enquirer';
// eslint-disable-next-line id-match
import { findLicense } from 'license';
import { Snippet } from '../../Snippet';
import { CopyrightGenerator } from './copyright_generator';
import { FileMapper } from './file_mapper';
import { CopyrightOptions } from './copyright_options';
export default class Copyright implements Snippet {
async start (cwd: string): Promise<void> {
const options = (await this.load_options_file (cwd))
|| (await this.gather_options ());
await FileMapper.map_all_files (
cwd,
Copyright.fix_file_license,
[ options ]
);
}
private async gather_options (): Promise<CopyrightOptions> {
const options = (new CopyrightOptions);
options.author = await new Input ({ message: 'author' })
.run ();
options.email = await new Input ({ message: 'email' })
.run ();
options.company = await new Input ({ message: 'company' })
.run ();
options.software = await new Input ({ message: 'software name' })
.run ();
options.has_license = await new Confirm ({
message:
'would you like to specify a license?'
})
.run ();
if (options.has_license) {
options.license = await new AutoComplete ({
name: 'license',
message: 'choose a license',
limit: 10,
choices: findLicense ('')
})
.run ();
}
return options;
}
private async load_options_file
(folder: string): Promise<CopyrightOptions|null> {
const file_path = path.join (folder, '.liconfig.json');
if (await fs.pathExists (file_path)) {
return JSON.parse (
await fs.readFile (file_path, 'utf-8')
);
}
return null;
}
private async save_options_file
(folder: string, options: CopyrightOptions): Promise<void> {
const file_path = path.join (folder, '.liconfig.json');
await fs.writeFile (file_path, JSON.stringify (options, null, 2), 'utf-8');
}
private static fix_file_license (
data: string,
filename: string,
[ options ]: [CopyrightOptions]
): string | null {
const regex = /\/\*\s+\*\sCopyright[\s\S]*?\*\/\n{0,2}/gu;
const shebang = /^#!.*?\n\n/gu;
const shebang_line = shebang.exec (data);
if (!(/\.(?:js|ts|mjs)$/u).test (filename) && !regex.test (data))
return null;
return (shebang_line ? shebang_line[0] : '')
+ CopyrightGenerator.get_copyright_notice (options)
+ data.replace (regex, '')
.replace (shebang, '');
}
}

View File

@ -0,0 +1,228 @@
/*
* 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>, March 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 };

66
lib/snippets/db/index.js Normal file
View File

@ -0,0 +1,66 @@
/*
* 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>, March 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 };

View File

@ -0,0 +1,78 @@
/*
* 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>, March 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 };

View File

@ -0,0 +1,39 @@
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"
}
}
}
}
}

View File

@ -0,0 +1,23 @@
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}'
}
}
}
}

View File

@ -0,0 +1,33 @@
/*
* 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>, March 2020
*/
/* 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' });
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'); }
});

127
lib/snippets/node/index.js Normal file
View File

@ -0,0 +1,127 @@
/*
* 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>, March 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 };

View File

@ -0,0 +1,24 @@
/*
* 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>, March 2020
*/
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
},
extends: [
'@scode'
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 2018
}
}

View File

@ -0,0 +1 @@
@scode:registry=https://npm.scode.ovh

View File

@ -0,0 +1,7 @@
/*
* 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>, March 2020
*/