Skip to content

Commit be21c8d

Browse files
committed
fix asynchrony
1 parent 23b5450 commit be21c8d

6 files changed

Lines changed: 43 additions & 27 deletions

File tree

‎packages/cli/src/commands/init/__tests__/editTemplate.test.js‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,28 @@ const FIXTURE_DIR = path.resolve(
1616
const PLACEHOLDER_NAME = 'PlaceholderName';
1717
const PROJECT_NAME = 'ProjectName';
1818

19-
function createTestEnv() {
19+
async function createTestEnv() {
2020
const TEST_DIR = `rncli-should-edit-template-${Date.now()}`;
2121
const tmpDir = os.tmpdir();
2222
const testPath = path.resolve(tmpDir, TEST_DIR);
2323

2424
fs.mkdirSync(testPath);
25-
copyFiles(FIXTURE_DIR, testPath);
25+
await copyFiles(FIXTURE_DIR, testPath);
2626

2727
return testPath;
2828
}
2929

30-
test('should edit template', () => {
31-
const testPath = createTestEnv();
30+
let testPath;
31+
32+
beforeEach(async () => {
33+
testPath = await createTestEnv();
34+
});
3235

36+
afterEach(() => {
37+
fs.removeSync(testPath);
38+
});
39+
40+
test('should edit template', () => {
3341
jest.spyOn(process, 'cwd').mockImplementation(() => testPath);
3442

3543
changePlaceholderInTemplate(PROJECT_NAME, PLACEHOLDER_NAME);
@@ -74,6 +82,4 @@ test('should edit template', () => {
7482
expect(
7583
snapshotDiff(fixtureTree, transformedTree, {contextLines: 1}),
7684
).toMatchSnapshot();
77-
78-
fs.removeSync(testPath);
7985
});

‎packages/cli/src/commands/init/__tests__/template.test.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ test('getTemplateConfig', () => {
5656
);
5757
});
5858

59-
test('copyTemplate', () => {
59+
test('copyTemplate', async () => {
6060
const TEMPLATE_DIR = 'some/dir';
6161
const CWD = '.';
6262

6363
jest.spyOn(path, 'resolve').mockImplementationOnce((...e) => e.join('/'));
6464
jest.spyOn(copyFiles, 'default').mockImplementationOnce(() => {});
6565
jest.spyOn(process, 'cwd').mockImplementationOnce(() => CWD);
6666

67-
copyTemplate(TEMPLATE_NAME, TEMPLATE_DIR, TEMPLATE_SOURCE_DIR);
67+
await copyTemplate(TEMPLATE_NAME, TEMPLATE_DIR, TEMPLATE_SOURCE_DIR);
6868

6969
expect(path.resolve).toHaveBeenCalledWith(
7070
TEMPLATE_SOURCE_DIR,

‎packages/cli/src/commands/init/editTemplate.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
import fs from 'fs-extra';
2+
import fs from 'fs';
33
import path from 'path';
44
import walk from '../../tools/walk';
55
import {logger} from '@react-native-community/cli-tools';
@@ -30,7 +30,7 @@ function renameFile(filePath: string, oldName: string, newName: string) {
3030

3131
logger.debug(`Renaming ${filePath} -> file:${newFileName}`);
3232

33-
fs.moveSync(filePath, newFileName);
33+
fs.renameSync(filePath, newFileName);
3434
}
3535

3636
function shouldRenameFile(filePath: string, nameToReplace: string) {

‎packages/cli/src/commands/init/init.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async function createFromExternalTemplate(
6363

6464
name = adjustNameIfUrl(name, templateSourceDir);
6565
const templateConfig = getTemplateConfig(name, templateSourceDir);
66-
copyTemplate(name, templateConfig.templateDir, templateSourceDir);
66+
await copyTemplate(name, templateConfig.templateDir, templateSourceDir);
6767

6868
loader.succeed();
6969
loader.start('Preparing template');
@@ -121,7 +121,11 @@ async function createFromReactNativeTemplate(
121121
loader.start('Copying template');
122122

123123
const templateConfig = getTemplateConfig(TEMPLATE_NAME, templateSourceDir);
124-
copyTemplate(TEMPLATE_NAME, templateConfig.templateDir, templateSourceDir);
124+
await copyTemplate(
125+
TEMPLATE_NAME,
126+
templateConfig.templateDir,
127+
templateSourceDir,
128+
);
125129

126130
loader.succeed();
127131
loader.start('Processing template');

‎packages/cli/src/commands/init/template.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function getTemplateConfig(
4040
return require(configFilePath);
4141
}
4242

43-
export function copyTemplate(
43+
export async function copyTemplate(
4444
templateName: string,
4545
templateDir: string,
4646
templateSourceDir: string,
@@ -54,7 +54,7 @@ export function copyTemplate(
5454

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

57-
copyFiles(templatePath, process.cwd());
57+
await copyFiles(templatePath, process.cwd());
5858
}
5959

6060
export function executePostInitScript(

‎packages/cli/src/tools/copyFiles.js‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ import walk from './walk';
1414
/**
1515
* Copy files (binary included) recursively.
1616
*/
17-
function copyFiles(srcPath: string, destPath: string) {
18-
walk(srcPath).forEach(absoluteSrcFilePath => {
19-
const relativeFilePath = path.relative(srcPath, absoluteSrcFilePath);
20-
copyFile(absoluteSrcFilePath, path.resolve(destPath, relativeFilePath));
21-
});
17+
async function copyFiles(srcPath: string, destPath: string) {
18+
return Promise.all(
19+
walk(srcPath).map(async absoluteSrcFilePath => {
20+
const relativeFilePath = path.relative(srcPath, absoluteSrcFilePath);
21+
await copyFile(
22+
absoluteSrcFilePath,
23+
path.resolve(destPath, relativeFilePath),
24+
);
25+
}),
26+
);
2227
}
2328

2429
/**
@@ -33,10 +38,13 @@ function copyFile(srcPath: string, destPath: string) {
3338
return;
3439
}
3540

36-
copyBinaryFile(srcPath, destPath, err => {
37-
if (err) {
38-
throw err;
39-
}
41+
return new Promise((resolve, reject) => {
42+
copyBinaryFile(srcPath, destPath, err => {
43+
if (err) {
44+
reject(err);
45+
}
46+
resolve(destPath);
47+
});
4048
});
4149
}
4250

@@ -47,17 +55,15 @@ function copyBinaryFile(srcPath, destPath, cb) {
4755
let cbCalled = false;
4856
const {mode} = fs.statSync(srcPath);
4957
const readStream = fs.createReadStream(srcPath);
58+
const writeStream = fs.createWriteStream(destPath);
5059
readStream.on('error', err => {
5160
done(err);
5261
});
53-
const writeStream = fs.createWriteStream(destPath);
5462
writeStream.on('error', err => {
5563
done(err);
5664
});
57-
writeStream.on('close', () => {
65+
readStream.on('close', () => {
5866
done();
59-
});
60-
writeStream.on('ready', () => {
6167
fs.chmodSync(destPath, mode);
6268
});
6369
readStream.pipe(writeStream);

0 commit comments

Comments
 (0)