-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create command to setup prettier. * Add eslint path (#15)
- Loading branch information
Showing
13 changed files
with
490 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,39 @@ | ||
export default function runEslint() { | ||
console.log("ESLint!"); | ||
import { log } from 'console'; | ||
import path from 'path'; | ||
import { fileURLToPath, URL } from 'url'; | ||
import chalk from 'chalk'; | ||
import * as eta from 'eta'; | ||
import fs from 'fs-extra'; | ||
import addDependency from '../util/addDependency'; | ||
import getProjectDir from '../util/getProjectDir'; | ||
import isEslintConfigured from '../util/isEslintConfigured'; | ||
import isPackageInstalled from '../util/isPackageInstalled'; | ||
import writeFile from '../util/writeFile'; | ||
|
||
const dirname = fileURLToPath(new URL('.', import.meta.url)); | ||
|
||
export default async function runEslint() { | ||
const projectDir = await getProjectDir(); | ||
|
||
if (await isEslintConfigured()) { | ||
log('eslint config already exists'); | ||
} else { | ||
const hasTypeScript = await isPackageInstalled('typescript'); | ||
|
||
const eslintConfigTemplate = await fs.readFile( | ||
path.join(dirname, 'templates', 'eslintrc.js.eta'), | ||
); | ||
|
||
const fileContents = eta.render(eslintConfigTemplate.toString(), { | ||
typescript: hasTypeScript, | ||
}); | ||
|
||
await writeFile(path.join(projectDir, '.eslintrc.js'), fileContents, { | ||
format: true, | ||
}); | ||
|
||
await addDependency('@thoughtbot/eslint-config', { dev: true }); | ||
|
||
log(chalk.green('🎉 ESLint successfully configured')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
"extends": [ | ||
"@thoughtbot/eslint-config/native", | ||
<% if(it.typescript) { %> | ||
"@thoughtbot/eslint-config/typescript" | ||
<% } %> | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { exec } from "child_process"; | ||
import * as fs from "fs-extra"; | ||
import * as path from "path"; | ||
import getProjectDir from "./getProjectDir"; | ||
import { exec } from 'child_process'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs-extra'; | ||
import getProjectDir from './getProjectDir'; | ||
|
||
export default async function addDependency(deps: string, { dev = false }) { | ||
const isYarn = await fs.exists(path.join(await getProjectDir(), "yarn.lock")); | ||
const isYarn = await fs.exists(path.join(await getProjectDir(), 'yarn.lock')); | ||
|
||
if (isYarn) { | ||
exec(`yarn add ${dev ? "--dev" : ""} ${deps}`); | ||
exec(`yarn add ${dev ? '--dev' : ''} ${deps}`); | ||
} else { | ||
exec(`npm install ${dev ? "--save-dev" : "--save"} ${deps}`); | ||
exec(`npm install ${dev ? '--save-dev' : '--save'} ${deps}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import path from 'path'; | ||
import fsExtra from 'fs-extra'; | ||
import getProjectDir from './getProjectDir'; | ||
import readPackageJson from './readPackageJson'; | ||
|
||
export default async function isEslintConfigured() { | ||
const packageJson = await readPackageJson(); | ||
|
||
const projectDir = await getProjectDir(); | ||
|
||
const hasEslintConfigInPackageJson = Object.prototype.hasOwnProperty.call( | ||
packageJson, | ||
'eslint', | ||
); | ||
const hasEslintrcJsFile = await fsExtra.exists( | ||
path.join(projectDir, '.eslintrc.js'), | ||
); | ||
const hasEslintJsonFile = await fsExtra.exists( | ||
path.join(projectDir, '.eslintrc.json'), | ||
); | ||
|
||
return hasEslintConfigInPackageJson || hasEslintrcJsFile || hasEslintJsonFile; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import readPackageJson from './readPackageJson'; | ||
|
||
export default async function isPackageInstalled(packageName: string) { | ||
const packageJson = await readPackageJson(); | ||
|
||
const isPackageInDevdependencies = Object.prototype.hasOwnProperty.call( | ||
packageJson.dependencies, | ||
packageName, | ||
); | ||
const isPackageInDependencies = Object.prototype.hasOwnProperty.call( | ||
packageJson.devDependencies, | ||
packageName, | ||
); | ||
return isPackageInDevdependencies || isPackageInDependencies; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import fs from "fs-extra"; | ||
import path from "path"; | ||
import { PackageJson } from "../types"; | ||
import getProjectDir from "./getProjectDir"; | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import { PackageJson } from '../types'; | ||
import getProjectDir from './getProjectDir'; | ||
|
||
export default async function readPackageJson() { | ||
const rootDir = await getProjectDir(); | ||
const pkg = await fs.readFile(path.join(rootDir, "package.json")); | ||
const pkg = await fs.readFile(path.join(rootDir, 'package.json')); | ||
return JSON.parse(pkg.toString()) as PackageJson; | ||
} |
Oops, something went wrong.