Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions __e2e__/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ test('init --template', () => {

// make sure we don't leave garbage
expect(fs.readdirSync(DIR)).toEqual(['TestInit']);
expect(fs.readdirSync(path.join(DIR, 'TestInit'))).toEqual(templateFiles);

let dirFiles = fs.readdirSync(path.join(DIR, 'TestInit'));
expect(dirFiles.length).toEqual(templateFiles.length);

for (var templateFile of templateFiles) {
expect(dirFiles.includes(templateFile)).toBe(true);
}

const pkgJson = require(path.join(DIR, 'TestInit', 'package.json'));
expect(pkgJson).toMatchSnapshot(
Expand All @@ -74,11 +80,14 @@ test('init --template file:/tmp/custom/template', () => {
'custom/template/template-dir/package.json': '{}',
'custom/template/template-dir/empty': '',
});
let templatePath = path.resolve(DIR, 'custom', 'template');
if (process.platform === 'win32')
templatePath = templatePath.split('\\').join('/');

const {stdout} = run(DIR, [
'init',
'--template',
`file://${path.resolve(DIR, 'custom', 'template')}`,
`file://${templatePath}`,
'TestInit',
]);

Expand All @@ -100,5 +109,11 @@ test('init --template with custom project path', () => {

// make sure we don't leave garbage
expect(fs.readdirSync(DIR)).toEqual([customPath]);
expect(fs.readdirSync(path.join(DIR, customPath))).toEqual(templateFiles);

let dirFiles = fs.readdirSync(path.join(DIR, customPath));
expect(dirFiles.length).toEqual(templateFiles.length);

for (var templateFile of templateFiles) {
expect(dirFiles.includes(templateFile)).toBe(true);
}
});
9 changes: 7 additions & 2 deletions __e2e__/legacyInit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ test('legacy init through react-native-cli', () => {
'package.json',
'yarn.lock',
];

const {stdout} = execa.sync('npx', ['react-native-cli', 'init', 'TestApp'], {
cwd: DIR,
});
Expand All @@ -43,7 +42,13 @@ test('legacy init through react-native-cli', () => {

// make sure we don't leave garbage
expect(fs.readdirSync(DIR)).toEqual(['TestApp']);
expect(fs.readdirSync(path.join(DIR, 'TestApp'))).toEqual(templateFiles);

let dirFiles = fs.readdirSync(path.join(DIR, 'TestApp'));
expect(dirFiles.length).toEqual(templateFiles.length);

for (var templateFile of templateFiles) {
expect(dirFiles.includes(templateFile)).toBe(true);
}

const pkgJson = require(path.join(DIR, 'TestApp', 'package.json'));
expect(pkgJson).toMatchObject({
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
"mkdirp": "^0.5.1",
"string-length": "^2.0.0",
"typescript": "^3.4.5"
},
"devDependencies": {
"slash": "^3.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import os from 'os';
import path from 'path';
import fs from 'fs-extra';
import snapshotDiff from 'snapshot-diff';
import slash from 'slash';
import walk from '../../../tools/walk';
import copyFiles from '../../../tools/copyFiles';
import {changePlaceholderInTemplate} from '../editTemplate';
Expand Down Expand Up @@ -80,6 +81,8 @@ test('should edit template', () => {
snapshotDiff(oldJavaFile, newJavaFile, {contextLines: 1}),
).toMatchSnapshot();
expect(
snapshotDiff(fixtureTree, transformedTree, {contextLines: 1}),
snapshotDiff(fixtureTree.map(slash), transformedTree.map(slash), {
contextLines: 1,
}),
).toMatchSnapshot();
});
4 changes: 4 additions & 0 deletions packages/cli/src/commands/init/__tests__/templateName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const RN_NPM_PACKAGE = 'react-native';
const ABS_RN_PATH = '/path/to/react-native';

test('supports file protocol with absolute path', async () => {
if (process.platform === 'win32') {
console.warn('[SKIP] Jest virtual mocks seem to be broken on Windows');
return;
}
jest.mock(
`${ABS_RN_PATH}/package.json`,
() => ({
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function setProjectDirectory(directory) {
throw new DirectoryAlreadyExistsError(directory);
}

await fs.emptyDir(directory);
fs.emptyDirSync(directory);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using await causes hang on Windows, that's why.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Geez, Windows is so weird

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It indeed is. 😂😂

}

try {
Expand Down
14 changes: 12 additions & 2 deletions packages/cli/src/commands/init/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import * as PackageManager from '../../tools/packageManager';
import {logger} from '@react-native-community/cli-tools';
import copyFiles from '../../tools/copyFiles';

const replacePathSepForRegex = (string: string) => {
if (path.sep === '\\') {
return string.replace(
/(\/|(.)?\\(?![[\]{}()*+?.^$|\\]))/g,
(_match, _, p2) => (p2 && p2 !== '\\' ? p2 + '\\\\' : '\\\\'),
);
}
return string;
};

export type TemplateConfig = {
placeholderName: string,
templateDir: string,
Expand Down Expand Up @@ -53,9 +63,9 @@ export async function copyTemplate(
);

logger.debug(`Copying template from ${templatePath}`);

let regexStr = path.resolve(templatePath, 'node_modules');
await copyFiles(templatePath, process.cwd(), {
exclude: [new RegExp(path.resolve(templatePath, 'node_modules'))],
exclude: [new RegExp(replacePathSepForRegex(regexStr))],
});
}

Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/commands/init/templateName.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ const VERSION_POSTFIX = /(.*)(-\d+\.\d+\.\d+)/;
const VERSIONED_PACKAGE = /(@?.+)(@)(.+)/;

function handleFileProtocol(filePath: string) {
const uri = new URL(filePath).pathname;

let uri = new URL(filePath).pathname;
if (process.platform === 'win32') {
// On Windows, the pathname has an extra leading / so remove that
uri = uri.substring(1);
}
return {
uri,
name: require(path.join(uri, 'package.json')).name,
Expand Down
14 changes: 12 additions & 2 deletions packages/cli/src/tools/__tests__/copyFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import path from 'path';
import copyFiles from '../copyFiles';
import {cleanup, getTempDirectory} from '../../../../../jest/helpers';

const replacePathSepForRegex = (string: string) => {
if (path.sep === '\\') {
return string.replace(
/(\/|(.)?\\(?![[\]{}()*+?.^$|\\]))/g,
(_match, _, p2) => (p2 && p2 !== '\\' ? p2 + '\\\\' : '\\\\'),
);
}
return string;
};

const DIR = getTempDirectory('copyFiles-test');

beforeEach(() => {
Expand Down Expand Up @@ -47,10 +57,10 @@ test('copies text and binary files from source to destination', async () => {

test('copies files from source to destination excluding directory', async () => {
const src = path.resolve(__dirname, './__fixtures__');
let regexStr = path.join(src, 'extraDir');
await copyFiles(src, DIR, {
exclude: [new RegExp(path.join(src, 'extraDir'))],
exclude: [new RegExp(replacePathSepForRegex(regexStr))],
});

expect(fs.readdirSync(DIR)).toMatchInlineSnapshot(`
Array [
"binary.keystore",
Expand Down
45 changes: 29 additions & 16 deletions packages/cli/src/tools/config/__tests__/index-test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
/**
* @flow
*/

import path from 'path';
import slash from 'slash';
import loadConfig from '..';

import {logger} from '@react-native-community/cli-tools';
import {
cleanup,
writeFiles,
getTempDirectory,
} from '../../../../../../jest/helpers';

import {logger} from '@react-native-community/cli-tools';

jest.mock('../resolveNodeModuleDir');

const DIR = getTempDirectory('resolve_config_path_test');

// Removes string from all key/values within an object
const removeString = (config, str) =>
JSON.parse(
JSON.stringify(config).replace(new RegExp(str, 'g'), '<<REPLACED>>'),
JSON.stringify(config, (_key, value) =>
typeof value === 'string'
? slash(value.replace(str, '<<REPLACED>>'))
: value,
),
);

beforeEach(() => {
Expand Down Expand Up @@ -302,21 +305,30 @@ test('does not use restricted "react-native" key to resolve config from package.
});

test('supports dependencies from user configuration with custom root and properties', () => {
const escapePathSeparator = (value: string) =>
path.sep === '\\' ? value.replace(/(\/|\\)/g, '\\\\') : value;

writeFiles(DIR, {
'node_modules/react-native/package.json': '{}',
'native-libs/local-lib/ios/LocalRNLibrary.xcodeproj/project.pbxproj': '',
'react-native.config.js': `module.exports = {
dependencies: {
'local-lib': {
root: "${DIR}/native-libs/local-lib",
platforms: {
ios: {
podspecPath: "custom-path"
}
}
},
'react-native.config.js': `
const path = require('path');
const root = path.resolve('${escapePathSeparator(
DIR,
)}', 'native-libs', 'local-lib');

module.exports = {
dependencies: {
'local-lib': {
root,
platforms: {
ios: {
podspecPath: "custom-path"
}
}
}`,
},
}
}`,
'package.json': `{
"dependencies": {
"react-native": "0.0.1"
Expand All @@ -325,6 +337,7 @@ test('supports dependencies from user configuration with custom root and propert
});

const {dependencies} = loadConfig(DIR);
console.log(dependencies['local-lib']);
expect(removeString(dependencies['local-lib'], DIR)).toMatchInlineSnapshot(`
Object {
"assets": Array [],
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/src/tools/config/findAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import glob from 'glob';
import path from 'path';

const findAssetsInFolder = folder =>
glob.sync(path.join(folder, '**'), {nodir: true});
const findAssetsInFolder = folder => {
let assets = glob.sync(path.join(folder, '**'), {nodir: true});
if (process.platform === 'win32') {
assets = assets.map(asset => asset.split('/').join('\\'));
}
return assets;
};

/**
* Given an array of assets folders, e.g. ['Fonts', 'Images'],
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8207,6 +8207,11 @@ slash@^2.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"
integrity sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==

slash@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==

slice-ansi@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/slice-ansi/-/slice-ansi-1.0.0.tgz#044f1a49d8842ff307aad6b505ed178bd950134d"
Expand Down