complete other snippets

This commit is contained in:
2020-04-18 17:24:41 +02:00
parent 3c44614298
commit f383ed0ce4
18 changed files with 213 additions and 550 deletions

@ -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 };

@ -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'));
}
}

@ -0,0 +1,4 @@
/node_modules/
/dist/
/.nyc_output/
/coverage/