Files
Lucas Bento c2fb83fb64 feat: support init with custom project directory (#365)
* Change `packageName` to `projectName` & add `projectPath` argument

* Add a way to specify a custom project path to init the app on

* Force the `init` command to create the project path recursively

* Add test for providing the project path on init

* Throw error if custom project path doesn’t exist and can’t be created

* Fix type error on `customProjectPath`

* Create directory before running the test with custom project path

* Use `mkdirp` to create project folder & Use `inquirer` to prompt for a directory replace

* Specify `directory` as an option

* Handle custom directory path after react logo

* Check if `ios` folder exists before trying to open it

* Update `init` tests

* Add `directory` type

* Remove unused `inquirer` from `init` test

* Only remove the project folder if it didn’t exist before running `init`

* Remove correct directory if it didn’t exist before `init`

* simplify

* make directory an argument instead of a flag

* update descriptions

* fix test

* use --directory after all

* address feedback; remove version handling from createFromTemplate

* flip the default
2019-05-17 18:25:11 +02:00

105 lines
2.5 KiB
JavaScript

// @flow
import fs from 'fs';
import path from 'path';
import {run, getTempDirectory, cleanup, writeFiles} from '../jest/helpers';
const DIR = getTempDirectory('command-init');
beforeEach(() => {
cleanup(DIR);
writeFiles(DIR, {});
});
afterEach(() => {
cleanup(DIR);
});
test('init --template fails without package name', () => {
const {stderr} = run(
DIR,
['init', '--template', 'react-native-new-template'],
{expectedFailure: true},
);
expect(stderr).toContain('missing required argument');
});
const templateFiles = [
'.buckconfig',
'.eslintrc.js',
'.flowconfig',
'.gitattributes',
// should be here, but it's not published yet
// '.gitignore',
'.watchmanconfig',
'App.js',
'__tests__',
'android',
'babel.config.js',
'index.js',
'ios',
'metro.config.js',
'node_modules',
'package.json',
'yarn.lock',
];
test('init --template', () => {
const {stdout} = run(DIR, [
'init',
'--template',
'react-native-new-template',
'TestInit',
]);
expect(stdout).toContain('Welcome to React Native!');
expect(stdout).toContain('Run instructions');
// make sure we don't leave garbage
expect(fs.readdirSync(DIR)).toEqual(['TestInit']);
expect(fs.readdirSync(path.join(DIR, 'TestInit'))).toEqual(templateFiles);
const pkgJson = require(path.join(DIR, 'TestInit', 'package.json'));
expect(pkgJson).toMatchSnapshot(
'package.json contains necessary configuration',
);
});
test('init --template file:/tmp/custom/template', () => {
writeFiles(DIR, {
'custom/template/template.config.js': `module.exports = {
placeholderName: 'HelloWorld',
templateDir: './template-dir',
};`,
'custom/template/package.json':
'{ "name": "template", "version": "0.0.1" }',
'custom/template/template-dir/package.json': '{}',
'custom/template/template-dir/empty': '',
});
const {stdout} = run(DIR, [
'init',
'--template',
`file://${path.resolve(DIR, 'custom', 'template')}`,
'TestInit',
]);
expect(stdout).toContain('Run instructions');
});
test('init --template with custom project path', () => {
const projectName = 'TestInit';
const customPath = 'custom-path';
run(DIR, [
'init',
'--template',
'react-native-new-template',
projectName,
'--directory',
'custom-path',
]);
// make sure we don't leave garbage
expect(fs.readdirSync(DIR)).toEqual([customPath]);
expect(fs.readdirSync(path.join(DIR, customPath))).toEqual(templateFiles);
});