Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: eslint #78

Merged
merged 10 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty big one for this project

},
};
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`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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