Skip to content

Commit

Permalink
feat: eslint (#78)
Browse files Browse the repository at this point in the history
* chore: add eslint

* chore: vscode prettier formating

* ci: lint workflow

* ci(lint): run on node 14

* chore: vscode recommended extensions

* chore: add eslint recommended plugin
  • Loading branch information
vltansky authored Nov 4, 2020
1 parent 892e025 commit e7a4db0
Show file tree
Hide file tree
Showing 7 changed files with 1,710 additions and 365 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
env: {
browser: true,
commonjs: true,
es2020: true,
},
extends: ["eslint:recommended", "airbnb/base", "plugin:prettier/recommended"],
parserOptions: {
ecmaVersion: 12,
},
rules: {
"no-console": "off",
},
};
33 changes: 33 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Eslint

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Get npm cache directory
id: npm-cache
run: |
echo "::set-output name=dir::$(npm config get cache)"
- uses: actions/cache@v2
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: "14.x"
- run: npm ci
- run: npm run lint
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"eslint.enable": true,
"eslint.validate": ["javascript"]
}
124 changes: 65 additions & 59 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,29 @@
"use strict";
const yargs_parser = require("yargs-parser");
const yargsParser = require("yargs-parser");
const path = require("path");
const chalk = require("chalk");
const langsList = require("./countries.json");
const inquirer = require("inquirer");
const fuzzy = require("fuzzy");
const ora = require("ora");
const { extract } = require("pacote");
const glob = require("fast-glob");
const fs = require("fs-extra");
const os = require("os");

const packageName = "html5-boilerplate";
const tempDir = os.tmpdir() + `/${packageName}-staging`;
const tempDir = `${os.tmpdir()}/${packageName}-staging`;
const elapsed = require("elapsed-time-logger");
const compareVersions = require("compare-versions");
const langsList = require("./countries.json");

let spinner;
inquirer.registerPrompt(
"autocomplete",
require("inquirer-autocomplete-prompt")
);
module.exports = async (argvs) => {
const argv = yargs_parser(argvs, {
alias: { release: ["r"], yes: ["y"] },
});
const timer = elapsed.start();
const version = (argv["release"] || "latest").toString();
const targetDir = path.resolve(argv["_"][0] || "./");
const override = await checkFolder(targetDir, argv);
if (!override) {
console.log(chalk.red("Aborted"));
return;
}
spinner = ora(
`Downloading ${packageName} version '${version}' to ${targetDir}`
).start();
await fs.ensureDir(tempDir);
try {
const { from: nameWithVersion } = await extract(
packageName + "@" + version,
tempDir,
{}
);
await fs.copy(tempDir + "/dist", targetDir);
const timerDownloaded = timer.get();
await onLoad(targetDir, version, argv);
spinner.succeed(
` ${nameWithVersion} copied to ${targetDir} in ${timerDownloaded}. Have fun!`
);
return;
} catch (err) {
if (err.code === "ETARGET") {
const msg = chalk.red(
`version '${err.wanted}' not found in npm registry\navailable versions:\n`
);
spinner.fail(msg + err.versions.reverse().join(" | "));
throw err.code;
}
spinner.fail("✖ Unexpected error");
throw new Error(err);
} finally {
await fs.remove(tempDir);
}
};

const checkFolder = async (targetDir, argv) => {
const folderExists = await fs.pathExists(targetDir);
if (!folderExists) {
return true;
}
if (argv["yes"] === true) {
if (!folderExists || argv.yes === true) {
return true;
}
const folderFiles = await fs.readdir(targetDir);
Expand All @@ -81,17 +36,23 @@ const checkFolder = async (targetDir, argv) => {
});
return override;
}
return true;
};

const onLoad = async (targetDir, version, argv) => {
// see https://github.com/mrmlnc/fast-glob#how-to-write-patterns-on-windows
const npmIgnoreFiles = await glob(
`${targetDir.replace(/\\/g, "/")}/**/.npmignore`
);
for (const npmIgnore of npmIgnoreFiles) {
await fs.rename(npmIgnore, npmIgnore.replace(/\.npmignore$/, ".gitignore"));
}
const skipPrompts = argv["yes"] === true;
await Promise.all(
npmIgnoreFiles.map((fileName) => {
return fs.rename(
fileName,
fileName.replace(/\.npmignore$/, ".gitignore")
);
})
);
const skipPrompts = argv.yes === true;

if (skipPrompts) {
return;
Expand All @@ -100,11 +61,11 @@ const onLoad = async (targetDir, version, argv) => {
const langListOut = [];
/* istanbul ignore if */
if (!argv.lang) {
for (const { title, value } of langsList) {
langsList.forEach(({ title, value }) => {
const text = `${title} (${value})`;
langListMap[text] = value;
langListOut.push(text);
}
});
langListOut.splice(1, 0, "Enter custom");
}
spinner.stop();
Expand All @@ -121,6 +82,7 @@ const onLoad = async (targetDir, version, argv) => {
type: "input",
name: "customLang",
message: "Enter custom language code",
// eslint-disable-next-line
when: ({ langChoice }) => !argv.lang && langChoice === langListOut[1],
},
{
Expand All @@ -135,10 +97,10 @@ const onLoad = async (targetDir, version, argv) => {
const lang = argv.lang || langListMap[langChoice] || customLang || "";
const removeJqueryFlag = removeJquery !== undefined ? removeJquery : false;
try {
const indexFile = targetDir + "/index.html";
const indexFile = `${targetDir}/index.html`;
const sourceHTML = await fs.readFile(indexFile, "utf-8");
let resultHTML = sourceHTML.replace(
/(<html.*lang=)\"([^"]*)\"/gi,
/(<html.*lang=)"([^"]*)"/gi,
`$1"${lang}"`
);
if (removeJqueryFlag) {
Expand All @@ -154,3 +116,47 @@ const onLoad = async (targetDir, version, argv) => {
throw new Error(err);
}
};

module.exports = async (argvs) => {
const argv = yargsParser(argvs, {
alias: { release: ["r"], yes: ["y"] },
});
const timer = elapsed.start();
const version = (argv.release || "latest").toString();
const targetDir = path.resolve(argv._[0] || "./");
const override = await checkFolder(targetDir, argv);
if (!override) {
console.log(chalk.red("Aborted"));
return;
}
spinner = ora(
`Downloading ${packageName} version '${version}' to ${targetDir}`
).start();
await fs.ensureDir(tempDir);
try {
const { from: nameWithVersion } = await extract(
`${packageName}@${version}`,
tempDir,
{}
);
await fs.copy(`${tempDir}/dist`, targetDir);
const timerDownloaded = timer.get();
await onLoad(targetDir, version, argv);
spinner.succeed(
` ${nameWithVersion} copied to ${targetDir} in ${timerDownloaded}. Have fun!`
);
return;
} catch (err) {
if (err.code === "ETARGET") {
const msg = chalk.red(
`version '${err.wanted}' not found in npm registry\navailable versions:\n`
);
spinner.fail(msg + err.versions.reverse().join(" | "));
throw err.code;
}
spinner.fail("✖ Unexpected error");
throw new Error(err);
} finally {
await fs.remove(tempDir);
}
};
Loading

0 comments on commit e7a4db0

Please sign in to comment.