-
-
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.
- Loading branch information
Stephen Hanson
committed
Oct 29, 2023
1 parent
34db957
commit d73ba6a
Showing
19 changed files
with
600 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ npm-debug.log | |
.idea/* | ||
.vscode/* | ||
.DS_Store | ||
/build |
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,3 @@ | ||
{ | ||
"singleQuote": true | ||
"singleQuote": true | ||
} |
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,25 @@ | ||
import actualfs from 'fs-extra'; | ||
import { fs } from 'memfs'; | ||
|
||
const DONT_MOCK_PATTERNS = ['templates/']; | ||
|
||
export default { | ||
...fs.promises, | ||
exists(path) { | ||
return new Promise((resolve) => { | ||
fs.exists(path, (exists) => resolve(exists)); | ||
}); | ||
}, | ||
readFile: (...args) => { | ||
const path = args[0]; | ||
const useActual = DONT_MOCK_PATTERNS.some((pattern) => | ||
path.includes(pattern), | ||
); | ||
|
||
if (useActual) { | ||
return actualfs.readFile(...args); | ||
} | ||
|
||
return fs.promises.readFile(...args); | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
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,12 +1,12 @@ | ||
#!/usr/bin/env node | ||
|
||
import { spawn } from "node:child_process"; | ||
import path from "node:path"; | ||
import { URL, fileURLToPath } from "node:url"; | ||
import { spawn } from 'node:child_process'; | ||
import path from 'node:path'; | ||
import { URL, fileURLToPath } from 'node:url'; | ||
|
||
const dirname = fileURLToPath(new URL(".", import.meta.url)); | ||
const dirname = fileURLToPath(new URL('.', import.meta.url)); | ||
const args = process.argv.slice(2); | ||
|
||
spawn("ts-node", [path.join(dirname, "../src/cli.ts"), ...args], { | ||
stdio: "inherit", | ||
spawn('ts-node', [path.join(dirname, '../src/cli.ts'), ...args], { | ||
stdio: 'inherit', | ||
}); |
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,75 @@ | ||
// import fs from 'fs'; | ||
import { fs as memfs } from 'memfs'; | ||
import { expect, test } from 'vitest'; | ||
import { fs, vol } from 'memfs'; | ||
import { Mock, afterEach, expect, test, vi } from 'vitest'; | ||
import addDependency from '../../util/addDependency'; | ||
import print from '../../util/print'; | ||
import addTypescript from '../typescript'; | ||
|
||
test('mocking file system works', async () => { | ||
memfs.writeFileSync('/hello.txt', 'World!'); | ||
expect(memfs.readFileSync('/hello.txt', 'utf8')).toEqual('World!'); | ||
vi.mock('fs-extra'); | ||
vi.mock('../../util/addDependency'); | ||
vi.mock('../../util/print', () => ({ | ||
// __esModule: true, | ||
default: vi.fn(), | ||
})); | ||
|
||
afterEach(() => { | ||
vol.reset(); | ||
(print as Mock).mockReset(); | ||
}); | ||
|
||
test('exits with message if tsconfig.json already exists', async () => { | ||
const json = { | ||
'package.json': '{}', | ||
'tsconfig.json': '1', | ||
}; | ||
vol.fromJSON(json, './'); | ||
|
||
await addTypescript(); | ||
expect(print).toHaveBeenCalledWith( | ||
expect.stringMatching(/tsconfig\.json already exists/), | ||
); | ||
|
||
// doesn't modify | ||
expect(fs.readFileSync('tsconfig.json', 'utf8')).toEqual('1'); | ||
}); | ||
|
||
test('writes new tsconfig.json, adds dependencies', async () => { | ||
vol.fromJSON({ | ||
'package.json': JSON.stringify({ | ||
dependencies: { | ||
expo: '1.0.0', | ||
}, | ||
}), | ||
}); | ||
|
||
await addTypescript(); | ||
|
||
expect(addDependency).toHaveBeenCalledWith('typescript @types/react', { | ||
dev: true, | ||
}); | ||
|
||
expect(fs.readFileSync('tsconfig.json', 'utf8')).toMatch( | ||
'"extends": "expo/tsconfig.base"', | ||
); | ||
|
||
expect(print).not.toHaveBeenCalledWith( | ||
expect.stringMatching(/already exists/), | ||
); | ||
}); | ||
|
||
test('exits with message if tsconfig.json already exists', () => { | ||
void addTypescript(); | ||
expect(true).toBe(true); | ||
test("doesn't extend expo/tsconfig.base if not an Expo project", async () => { | ||
vol.fromJSON({ | ||
'package.json': JSON.stringify({ | ||
dependencies: {}, | ||
}), | ||
}); | ||
|
||
await addTypescript(); | ||
|
||
expect(addDependency).toHaveBeenCalledWith('typescript @types/react', { | ||
dev: true, | ||
}); | ||
|
||
expect(fs.readFileSync('tsconfig.json', 'utf8')).not.toMatch( | ||
'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
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
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,24 +1,24 @@ | ||
import readPackageJson from "./readPackageJson"; | ||
import readPackageJson from './readPackageJson'; | ||
|
||
type ProjectType = "expo-bare" | "expo-managed" | "react-native"; | ||
type ProjectType = 'expo-bare' | 'expo-managed' | 'react-native'; | ||
|
||
export default async function getProjectType(): Promise<ProjectType> { | ||
const packageJson = await readPackageJson(); | ||
const hasExpo = hasProperty(packageJson.dependencies, "expo"); | ||
const hasExpo = hasProperty(packageJson.dependencies, 'expo'); | ||
const hasReactNativeUnimodules = hasProperty( | ||
packageJson.dependencies, | ||
"react-native-unimodules" | ||
'react-native-unimodules', | ||
); | ||
if (hasExpo) { | ||
return hasReactNativeUnimodules ? "expo-bare" : "expo-managed"; | ||
return hasReactNativeUnimodules ? 'expo-bare' : 'expo-managed'; | ||
} | ||
|
||
return "react-native"; | ||
return 'react-native'; | ||
} | ||
|
||
function hasProperty( | ||
object: Record<string, unknown> | undefined, | ||
property: string | ||
property: string, | ||
) { | ||
return Object.prototype.hasOwnProperty.call(object, property); | ||
} |
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,3 @@ | ||
export default function print(...args: unknown[]) { | ||
return console.log(...args); | ||
} |
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 |
---|---|---|
|
@@ -14,5 +14,5 @@ | |
"esm": true, | ||
"experimentalSpecifierResolution": "node" | ||
}, | ||
"include": ["./**/*"] | ||
"include": ["./**/*.ts"] | ||
} |
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,7 @@ | ||
import { configDefaults, defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
exclude: [...configDefaults.exclude, 'build/**/*'], | ||
}, | ||
}); |
Oops, something went wrong.