@xorsula/create-tsnode-app
Version:
Create tsnode app with elint linting and some sane defaults
49 lines (38 loc) ⢠11.3 kB
JavaScript
#!/usr/bin/env node
'use strict'
const path = require('path');
const fs = require('fs');
const {execSync} = require('child_process');
if (process.argv.length < 3) {
console.log('Specify project name please')
return;
}
console.log(`Project name is: ${JSON.stringify(process.argv[2])}`)
fs.mkdirSync(process.argv[2])
process.chdir(process.argv[2])
execSync(`npm init -y`)
// this is bad, todo find a better way
const contents = fs.readFileSync('package.json', {encoding: 'utf-8', flag: 'r'}).replace('index.js', 'index.ts');
fs.writeFileSync('package.json', contents);
console.log('copying files');
[
['.eslintignore', 'node_modules\n*.js\n.idea\nbuild\nlib\ndist\n.vscode\n'],
['.eslintrc.js', '/*\nš Hi! This file was autogenerated by tslint-to-eslint-config.\nhttps://github.com/typescript-eslint/tslint-to-eslint-config\n\nIt represents the closest reasonable ESLint configuration to this\nprojects original TSLint configuration.\n\nWe recommend eventually switching this configuration to extend from\nthe recommended rulesets in typescript-eslint.\nhttps://github.com/typescript-eslint/tslint-to-eslint-config/blob/master/docs/FAQs.md\n\nHappy linting! š\n*/\nmodule.exports = {\n "env": {\n "browser": true,\n "es6": true,\n "node": true\n },\n "extends": [\n "plugin:@typescript-eslint/recommended",\n "plugin:@typescript-eslint/recommended-requiring-type-checking"\n ],\n "parser": "@typescript-eslint/parser",\n "parserOptions": {\n "project": "tsconfig.json",\n "sourceType": "module"\n },\n "plugins": [\n "eslint-plugin-jsdoc",\n "eslint-plugin-prefer-arrow",\n "eslint-plugin-import",\n "@typescript-eslint",\n "@typescript-eslint/tslint"\n ],\n "rules": {\n "@typescript-eslint/adjacent-overload-signatures": "error",\n "@typescript-eslint/array-type": [\n "error",\n {\n "default": "array-simple"\n }\n ],\n "@typescript-eslint/ban-types": [\n "error",\n {\n "types": {\n "Object": {\n "message": "Avoid using the `Object` type. Did you mean `object`?"\n },\n "Function": {\n "message": "Avoid using the `Function` type. Prefer a specific function type, like `() => void`."\n },\n "Boolean": {\n "message": "Avoid using the `Boolean` type. Did you mean `boolean`?"\n },\n "Number": {\n "message": "Avoid using the `Number` type. Did you mean `number`?"\n },\n "String": {\n "message": "Avoid using the `String` type. Did you mean `string`?"\n },\n "Symbol": {\n "message": "Avoid using the `Symbol` type. Did you mean `symbol`?"\n }\n }\n }\n ],\n "@typescript-eslint/consistent-type-assertions": "error",\n "@typescript-eslint/consistent-type-definitions": "error",\n "@typescript-eslint/dot-notation": "error",\n "@typescript-eslint/explicit-member-accessibility": [\n "error",\n {\n "accessibility": "explicit"\n }\n ],\n "@typescript-eslint/indent": [\n "error",\n 2,\n {\n "FunctionDeclaration": {\n "parameters": "first"\n },\n "FunctionExpression": {\n "parameters": "first"\n }\n }\n ],\n "@typescript-eslint/member-delimiter-style": [\n "error",\n {\n "multiline": {\n "delimiter": "semi",\n "requireLast": true\n },\n "singleline": {\n "delimiter": "semi",\n "requireLast": false\n }\n }\n ],\n "@typescript-eslint/member-ordering": "error",\n "@typescript-eslint/naming-convention": "error",\n "@typescript-eslint/no-empty-function": "error",\n "@typescript-eslint/no-empty-interface": "error",\n "@typescript-eslint/no-explicit-any": "off",\n "@typescript-eslint/no-misused-new": "error",\n "@typescript-eslint/no-namespace": "error",\n "@typescript-eslint/no-parameter-properties": "off",\n "@typescript-eslint/no-shadow": [\n "error",\n {\n "hoist": "all"\n }\n ],\n "@typescript-eslint/no-unused-expressions": "error",\n "@typescript-eslint/no-use-before-define": "off",\n "@typescript-eslint/no-var-requires": "error",\n "@typescript-eslint/prefer-for-of": "error",\n "@typescript-eslint/prefer-function-type": "error",\n "@typescript-eslint/prefer-namespace-keyword": "error",\n "@typescript-eslint/quotes": [\n "error",\n "single",\n {\n "avoidEscape": true\n }\n ],\n "@typescript-eslint/semi": [\n "error",\n "always"\n ],\n "@typescript-eslint/triple-slash-reference": [\n "error",\n {\n "path": "always",\n "types": "prefer-import",\n "lib": "always"\n }\n ],\n "@typescript-eslint/type-annotation-spacing": "error",\n "@typescript-eslint/unified-signatures": "error",\n "arrow-body-style": "error",\n "arrow-parens": [\n "error",\n "always"\n ],\n "brace-style": [\n "error",\n "1tbs"\n ],\n "comma-dangle": [\n "error",\n "always-multiline"\n ],\n "complexity": "off",\n "constructor-super": "error",\n "curly": "error",\n "eol-last": "error",\n "eqeqeq": [\n "error",\n "smart"\n ],\n "guard-for-in": "error",\n "id-blacklist": [\n "error",\n "any",\n "Number",\n "number",\n "String",\n "string",\n "Boolean",\n "boolean",\n "Undefined",\n "undefined"\n ],\n "id-match": "error",\n "import/order": "error",\n "jsdoc/check-alignment": "error",\n "jsdoc/check-indentation": "error",\n "jsdoc/newline-after-description": "error",\n "max-classes-per-file": [\n "error",\n 1\n ],\n "max-len": [\n "error",\n {\n "code": 120\n }\n ],\n "new-parens": "error",\n "no-bitwise": "error",\n "no-caller": "error",\n "no-cond-assign": "error",\n "no-console": "off",\n "no-debugger": "error",\n "no-empty": "error",\n "no-eval": "error",\n "no-fallthrough": "off",\n "no-invalid-this": "off",\n "no-multiple-empty-lines": "error",\n "no-new-wrappers": "error",\n "no-throw-literal": "error",\n "no-trailing-spaces": "error",\n "no-undef-init": "error",\n "no-underscore-dangle": "error",\n "no-unsafe-finally": "error",\n "no-unused-labels": "error",\n "no-var": "error",\n "object-shorthand": "error",\n "one-var": [\n "error",\n "never"\n ],\n "prefer-arrow/prefer-arrow-functions": "error",\n "prefer-const": "error",\n "quote-props": [\n "error",\n "consistent-as-needed"\n ],\n "radix": "error",\n "space-before-function-paren": [\n "error",\n {\n "anonymous": "never",\n "asyncArrow": "always",\n "named": "never"\n }\n ],\n "spaced-comment": [\n "error",\n "always",\n {\n "markers": [\n "/"\n ]\n }\n ],\n "use-isnan": "error",\n "valid-typeof": "off",\n "@typescript-eslint/tslint/config": [\n "error",\n {\n "rules": {\n "import-spacing": true,\n "object-literal-sort-keys": false,\n "whitespace": [\n true,\n "check-branch",\n "check-decl",\n "check-operator",\n "check-separator",\n "check-type",\n "check-typecast"\n ]\n }\n }\n ]\n }\n};\n'],
['index.ts', '/**\n *\n * TODO for webstorm users\n * 1. Go to preferences, search for eslint, choose automatic configuration and enable "run eslint on save".\n * 2. Click "Add Configuration in the bar on top.\n * 2.1 Click "+" in upper left corner and select "Node.js"\n * 2.2 Node interpreter should be already selected for you, if not select one.\n * + this assumes you used nvm to install node. If not use "npm i node"\n * 2.3 Add "--require ts-node/register" to node parameters\n * 2.3 Select "index.ts" as entry file\n * 2.4 Click ok and you are ready to go\n * You may also have to manually configure eslint location.\n * Also change indent size to 2 if that\'s your thing, or edit the eslintrc.\n */\n\n//this is not how to decalre a function and eslint should scream at you\nfunction fun() {\n return 42;\n}\n\n// remove this comment hit ctrl + s, and webstorm (eslint) will add semicolons automatically\nconst obj: {name: string, age: number} = {name: "jane doe", age: 50}\n\nconsole.log(obj.name + fun())\n\n'],
['tsconfig.json', '{\n "compilerOptions": {\n "module": "commonjs",\n "esModuleInterop": true,\n "target": "es6",\n "moduleResolution": "node",\n "sourceMap": true,\n "outDir": "dist",\n "resolveJsonModule": true\n },\n "lib": ["es2015"]\n }\n'],
['.gitignore', '.idea\nnode_modules\n.vscode\n'],
['.prettierrc.js', 'module.exports = {\n bracketSpacing: false,\n jsxBracketSameLine: true,\n singleQuote: true,\n trailingComma: "all",\n endOfLine: "auto",\n printWidth: 120,\n};\n']
].forEach(([f, contents]) => fs.writeFileSync(f, contents)) //todo maybe parallelize
console.log('copying done\ninstalling...');
[
'npm i @types/node typescript ts-node',
'npm i -D @typescript-eslint/eslint-plugin @typescript-eslint/eslint-plugin-tslint @typescript-eslint/parser eslint eslint-plugin-import eslint-plugin-jsdoc eslint-plugin-prefer-arrow tslint',
].forEach((command) => {
try {
console.log(`Executing: ${command}`)
execSync(command);
} catch (e) {
console.log(e)
console.log('Sorry something went wrong. Try to read the errors, delete created dir and try again.')
process.exit(1);
}})