Skip to content

Commit

Permalink
feat(utils): add util that gets the React version of a project, if ex…
Browse files Browse the repository at this point in the history
…ists
  • Loading branch information
arthurdenner committed Nov 5, 2019
1 parent f1a2643 commit cf6718c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/createEslintConfig.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import fs from 'fs';
import path from 'path';
import { CLIEngine } from 'eslint';
import { PackageJson } from './types';
import { getReactVersion } from './utils';

interface CreateEslintConfigArgs {
pkg: PackageJson;
rootDir: string;
writeFile: boolean;
}
export function createEslintConfig({
pkg,
rootDir,
writeFile,
}: CreateEslintConfigArgs): CLIEngine.Options['baseConfig'] {
const isReactLibrary = Boolean(getReactVersion(pkg));

console.log(isReactLibrary);

const config = {
extends: [
'react-app',
Expand Down
10 changes: 3 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,13 @@ import { concatAllArray } from 'jpjs';
import getInstallCmd from './getInstallCmd';
import getInstallArgs from './getInstallArgs';
import { Input, Select } from 'enquirer';
import { TsdxOptions } from './types';
import { PackageJson, TsdxOptions } from './types';
import { createProgressEstimator } from './createProgressEstimator';
const pkg = require('../package.json');

const prog = sade('tsdx');

let appPackageJson: {
name: string;
source?: string;
jest?: any;
eslint?: any;
};
let appPackageJson: PackageJson;

try {
appPackageJson = fs.readJSONSync(resolveApp('package.json'));
Expand Down Expand Up @@ -606,6 +601,7 @@ prog
const cli = new CLIEngine({
baseConfig: {
...createEslintConfig({
pkg: appPackageJson,
rootDir: paths.appRoot,
writeFile: opts['write-file'],
}),
Expand Down
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ export interface TsdxOptions {
// Is this the very first rollup config (and thus should one-off metadata be extracted)?
writeMeta?: boolean;
}

export interface PackageJson {
name: string;
source?: string;
jest?: any;
eslint?: any;
dependencies?: { [packageName: string]: string };
devDependencies?: { [packageName: string]: string };
}
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs-extra';
import path from 'path';
import camelCase from 'camelcase';
import { PackageJson } from './types';

// Remove the package name scope if it exists
export const removeScope = (name: string) => name.replace(/^@.*\//, '');
Expand Down Expand Up @@ -35,3 +36,13 @@ export function clearConsole() {
process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H'
);
}

export function getReactVersion({
dependencies,
devDependencies,
}: PackageJson) {
return (
(dependencies && dependencies.react) ||
(devDependencies && devDependencies.react)
);
}

0 comments on commit cf6718c

Please sign in to comment.