-
-
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.
Add command for installing TypeScript (#3)
- Loading branch information
Stephen Hanson
authored
May 16, 2023
1 parent
0c40068
commit 50aa180
Showing
11 changed files
with
263 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "esNext", | ||
"lib": ["esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
"baseUrl": ".", | ||
"jsx": "react-native", | ||
"noFallthroughCasesInSwitch": true, | ||
"paths": { | ||
"src/*": ["src/*"], | ||
"assets/*": ["assets/*"] | ||
} | ||
}, | ||
"include": ["src/**/*", "*.js", ".*.js"], | ||
"exclude": ["node_modules"], | ||
<% if(it.expo) { %> | ||
"extends": "expo/tsconfig.base" | ||
<% } %> | ||
} |
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,46 @@ | ||
import chalk from "chalk"; | ||
import { log } from "console"; | ||
import * as eta from "eta"; | ||
import fs from "fs-extra"; | ||
import { fileURLToPath, URL } from "url"; | ||
import path from "path"; | ||
import addDependency from "../util/addDependency"; | ||
import getProjectDir from "../util/getProjectDir"; | ||
import getProjectType from "../util/getProjectType"; | ||
import writeFile from "../util/writeFile"; | ||
|
||
// for manual testing, change this to another name so doesn't conflict | ||
// with project's tsconfig.json | ||
const tsConfig = "tsconfig.json"; | ||
const __dirname = fileURLToPath(new URL(".", import.meta.url)); | ||
|
||
export default async function addTypescript() { | ||
const projectDir = await getProjectDir(); | ||
|
||
if (await fs.exists(path.join(projectDir, tsConfig))) { | ||
log( | ||
chalk.yellow("tsconfig.json already exists, exiting.") + | ||
"\nIf you would like to perform a fresh TypeScript install, delete this file and rerun the script.\n" | ||
); | ||
return; | ||
} | ||
|
||
addDependency("typescript @types/react", { dev: true }); | ||
|
||
const projectType = await getProjectType(); | ||
const template = await fs.readFile( | ||
path.join(__dirname, "templates", "tsconfig.json.eta") | ||
); | ||
const fileContents = eta.render(template.toString(), { | ||
expo: projectType === "expo-bare" || projectType === "expo-managed", | ||
}); | ||
|
||
await writeFile(path.join(projectDir, tsConfig), fileContents, { | ||
format: true, | ||
}); | ||
|
||
log( | ||
chalk.green("\n🎉 TypeScript successfully configured") + | ||
"\nConsider renaming your existing JS files as .ts or .tsx.\n" | ||
); | ||
} |
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,14 @@ | ||
import * as fs from "fs-extra"; | ||
import getProjectDir from "./getProjectDir"; | ||
import * as path from "path"; | ||
import { exec } from "child_process"; | ||
|
||
export default async function addDependency(deps: string, { dev = false }) { | ||
const isYarn = await fs.exists(path.join(await getProjectDir(), "yarn.lock")); | ||
|
||
if (isYarn) { | ||
exec(`yarn add ${dev ? "--dev" : ""} ${deps}`); | ||
} else { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { exec } from "child_process"; | ||
|
||
export default async function formatFile(filePath: string) { | ||
exec(`npx prettier --write '${filePath}'`); | ||
} |
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,28 @@ | ||
import fs from "fs-extra"; | ||
import path from "path"; | ||
|
||
export default async function getProjectDir( | ||
base: string = process.cwd() | ||
): Promise<string> { | ||
let previous = null; | ||
let dir = base; | ||
|
||
do { | ||
try { | ||
// This will throw if there is no package.json in the directory | ||
await fs.readFile(path.join(dir, "package.json")); | ||
|
||
// if didn't throw, package.json exists, return dir | ||
return dir; | ||
} catch { | ||
// Expected to throw if no package.json is present | ||
} finally { | ||
previous = dir; | ||
dir = path.dirname(dir); | ||
} | ||
} while (dir !== previous); | ||
|
||
throw new Error( | ||
"No project found. Ensure you are inside of a project directory with a package.json file." | ||
); | ||
} |
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,16 @@ | ||
import readPackageJson from "./readPackageJson"; | ||
|
||
type ProjectType = "expo-bare" | "expo-managed" | "react-native"; | ||
|
||
export default async function getProjectType(): Promise<ProjectType> { | ||
const packageJson = await readPackageJson(); | ||
const hasExpo = packageJson.dependencies?.hasOwnProperty("expo"); | ||
const hasReactNativeUnimodules = packageJson.dependencies?.hasOwnProperty( | ||
"react-native-unimodules" | ||
); | ||
if (hasExpo) { | ||
return hasReactNativeUnimodules ? "expo-bare" : "expo-managed"; | ||
} | ||
|
||
return "react-native"; | ||
} |
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,9 @@ | ||
import path from "path"; | ||
import getProjectDir from "./getProjectDir"; | ||
import fs from "fs-extra"; | ||
|
||
export default async function readPackageJson() { | ||
const rootDir = await getProjectDir(); | ||
const pkg = await fs.readFile(path.join(rootDir, "package.json")); | ||
return JSON.parse(pkg.toString()); | ||
} |
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,20 @@ | ||
import chalk from "chalk"; | ||
import fs from "fs-extra"; | ||
import formatFile from "./formatFile"; | ||
|
||
type Options = { | ||
format?: boolean; | ||
}; | ||
|
||
export default async function writeFile( | ||
filePath: string, | ||
contents: string, | ||
{ format = false }: Options = {} | ||
) { | ||
console.log(chalk.bold(`🔨 Creating ${filePath}`)); | ||
fs.writeFile(filePath, contents); | ||
|
||
if (format) { | ||
await formatFile(filePath); | ||
} | ||
} |
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