-
Notifications
You must be signed in to change notification settings - Fork 379
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
1 parent
3ca052f
commit afdc03c
Showing
9 changed files
with
310 additions
and
11 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
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,78 @@ | ||
// @flow | ||
import path from "path" | ||
import fs from "fs" | ||
import program from "commander" | ||
import { execSync } from "child_process" | ||
import inquirer from "inquirer" | ||
import chalk from "chalk" | ||
|
||
import { projectType, detect } from "./api/detect" | ||
|
||
function hasYarn() { | ||
return fs.existsSync(path.resolve("yarn.lock")) | ||
} | ||
|
||
function makeInstall() { | ||
const withYarn = hasYarn() | ||
|
||
return (packageName, dev = false) => | ||
withYarn | ||
? `yarn add ${dev ? "--dev " : ""}${packageName}` | ||
: `npm install ${dev ? "--save-dev" : "--save"} ${packageName}` | ||
} | ||
|
||
export async function installPackages(dryRun: boolean) { | ||
const install = makeInstall() | ||
|
||
const type = detect() | ||
const usesReact = type === projectType.CRA || type === projectType.REACT | ||
|
||
const packages = [ | ||
...(usesReact | ||
? [["@lingui/react"], ["@lingui/babel-preset-react", true]] | ||
: [["@lingui/core"], ["@lingui/babel-preset-js", true]]) | ||
].filter(Boolean) | ||
|
||
const verbosePackages = packages | ||
.map(([packageName]) => chalk.yellow(packageName)) | ||
.join(", ") | ||
const { confirm } = await inquirer.prompt({ | ||
type: "confirm", | ||
name: "confirm", | ||
message: `Do you want to install ${verbosePackages}?` | ||
}) | ||
|
||
if (!confirm) return false | ||
|
||
const commands = packages.map(([packageName, dev]) => | ||
install(packageName, dev) | ||
) | ||
if (dryRun) { | ||
commands.forEach(command => console.log(command)) | ||
} else { | ||
commands.forEach(command => execSync(command)) | ||
} | ||
|
||
return true | ||
} | ||
|
||
type CommandInit = {| | ||
dryRun: boolean | ||
|} | ||
|
||
export async function command(program: CommandInit) { | ||
await installPackages(program.dryRun) | ||
|
||
return true | ||
} | ||
|
||
if (require.main === module) { | ||
program | ||
.option( | ||
"--dry-run", | ||
"Output commands that would be run, but don't execute them." | ||
) | ||
.parse(process.argv) | ||
|
||
command(program) | ||
} |
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,121 @@ | ||
import mockFs from "mock-fs" | ||
import { installPackages } from "./lingui-init" | ||
import { mockConsole } from "./mocks" | ||
import inquirer from "inquirer" | ||
|
||
describe("lingui init", function() { | ||
let inquirerPrompt | ||
|
||
beforeAll(() => { | ||
inquirerPrompt = inquirer.prompt | ||
inquirer.prompt = jest.fn() | ||
}) | ||
|
||
afterAll(() => { | ||
inquirer.prompt = inquirerPrompt | ||
}) | ||
|
||
afterEach(() => { | ||
mockFs.restore() | ||
inquirer.prompt.mockReset() | ||
}) | ||
|
||
describe("install packages", function() { | ||
beforeEach(() => { | ||
inquirer.prompt = jest.fn(prompt => | ||
Promise.resolve({ [prompt.name]: true }) | ||
) | ||
}) | ||
|
||
it("should use yarn when available", () => { | ||
mockFs({ | ||
"yarn.lock": mockFs.file(), | ||
"package.json": JSON.stringify({ | ||
dependencies: {} | ||
}) | ||
}) | ||
|
||
expect.assertions(3) | ||
return mockConsole(console => | ||
installPackages(true).then(result => { | ||
expect(result).toBeTruthy() | ||
expect(console.log).toBeCalledWith("yarn add @lingui/core") | ||
expect(console.log).toBeCalledWith( | ||
"yarn add --dev @lingui/babel-preset-js" | ||
) | ||
}) | ||
) | ||
}) | ||
|
||
it("should use npm when yarn isn't available", () => { | ||
mockFs({ | ||
"package.json": JSON.stringify({ | ||
dependencies: {} | ||
}) | ||
}) | ||
|
||
expect.assertions(3) | ||
return mockConsole(console => | ||
installPackages(true) | ||
.then(result => { | ||
expect(result).toBeTruthy() | ||
expect(console.log).toBeCalledWith( | ||
"npm install --save @lingui/core" | ||
) | ||
expect(console.log).toBeCalledWith( | ||
"npm install --save-dev @lingui/babel-preset-js" | ||
) | ||
}) | ||
.catch(error => console.log({ error })) | ||
) | ||
}) | ||
|
||
it("should detect create-react-app project", () => { | ||
mockFs({ | ||
"package.json": JSON.stringify({ | ||
dependencies: { | ||
"react-scripts": "2.0.0" | ||
} | ||
}) | ||
}) | ||
|
||
expect.assertions(3) | ||
return mockConsole(console => | ||
installPackages(true) | ||
.then(result => { | ||
expect(result).toBeTruthy() | ||
expect(console.log).toBeCalledWith( | ||
"npm install --save @lingui/react" | ||
) | ||
expect(console.log).toBeCalledWith( | ||
"npm install --save-dev @lingui/babel-preset-react" | ||
) | ||
}) | ||
.catch(error => console.log({ error })) | ||
) | ||
}) | ||
|
||
it("should install core only for other projects", () => { | ||
mockFs({ | ||
"package.json": JSON.stringify({ | ||
dependencies: {} | ||
}) | ||
}) | ||
|
||
expect.assertions(3) | ||
return mockConsole(console => | ||
installPackages(true) | ||
.then(result => { | ||
expect(result).toBeTruthy() | ||
expect(console.log).toBeCalledWith( | ||
"npm install --save @lingui/core" | ||
) | ||
expect(console.log).toBeCalledWith( | ||
"npm install --save-dev @lingui/babel-preset-js" | ||
) | ||
}) | ||
.catch(error => console.log({ error })) | ||
) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.