@react-native-windows/cli 0.73.2 → 0.74.0-preview.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/lib-commonjs/commands/autolinkWindows/autolinkWindows.d.ts +87 -87
  2. package/lib-commonjs/commands/autolinkWindows/autolinkWindows.js +654 -654
  3. package/lib-commonjs/commands/autolinkWindows/autolinkWindowsOptions.d.ts +14 -14
  4. package/lib-commonjs/commands/autolinkWindows/autolinkWindowsOptions.js +32 -32
  5. package/lib-commonjs/commands/codegenWindows/codegenWindows.d.ts +27 -27
  6. package/lib-commonjs/commands/codegenWindows/codegenWindows.js +205 -205
  7. package/lib-commonjs/commands/codegenWindows/codegenWindowsOptions.d.ts +12 -12
  8. package/lib-commonjs/commands/codegenWindows/codegenWindowsOptions.js +22 -22
  9. package/lib-commonjs/commands/config/configUtils.d.ts +123 -123
  10. package/lib-commonjs/commands/config/configUtils.js +380 -380
  11. package/lib-commonjs/commands/config/dependencyConfig.d.ts +37 -37
  12. package/lib-commonjs/commands/config/dependencyConfig.js +227 -227
  13. package/lib-commonjs/commands/config/projectConfig.d.ts +27 -27
  14. package/lib-commonjs/commands/config/projectConfig.js +180 -180
  15. package/lib-commonjs/commands/healthCheck/healthCheckList.d.ts +6 -6
  16. package/lib-commonjs/commands/healthCheck/healthCheckList.js +20 -20
  17. package/lib-commonjs/commands/healthCheck/healthCheckList.js.map +1 -1
  18. package/lib-commonjs/commands/healthCheck/healthChecks.d.ts +7 -7
  19. package/lib-commonjs/commands/healthCheck/healthChecks.js +123 -123
  20. package/lib-commonjs/commands/initWindows/initWindows.d.ts +46 -46
  21. package/lib-commonjs/commands/initWindows/initWindows.js +243 -243
  22. package/lib-commonjs/commands/initWindows/initWindowsOptions.d.ts +15 -15
  23. package/lib-commonjs/commands/initWindows/initWindowsOptions.js +37 -37
  24. package/lib-commonjs/commands/runWindows/runWindows.d.ts +10 -10
  25. package/lib-commonjs/commands/runWindows/runWindows.js +321 -321
  26. package/lib-commonjs/commands/runWindows/runWindowsOptions.d.ts +56 -56
  27. package/lib-commonjs/commands/runWindows/runWindowsOptions.js +132 -132
  28. package/lib-commonjs/e2etest/autolink.test.d.ts +6 -6
  29. package/lib-commonjs/e2etest/autolink.test.js +366 -366
  30. package/lib-commonjs/e2etest/dependencyConfig.test.d.ts +6 -6
  31. package/lib-commonjs/e2etest/dependencyConfig.test.js +129 -129
  32. package/lib-commonjs/e2etest/healthChecks.test.d.ts +6 -6
  33. package/lib-commonjs/e2etest/healthChecks.test.js +30 -30
  34. package/lib-commonjs/e2etest/initWindows.test.d.ts +6 -6
  35. package/lib-commonjs/e2etest/initWindows.test.js +42 -42
  36. package/lib-commonjs/e2etest/projectConfig.test.d.ts +6 -6
  37. package/lib-commonjs/e2etest/projectConfig.test.js +110 -110
  38. package/lib-commonjs/e2etest/projectConfig.utils.d.ts +8 -8
  39. package/lib-commonjs/e2etest/projectConfig.utils.js +76 -76
  40. package/lib-commonjs/e2etest/runWindows.test.d.ts +6 -6
  41. package/lib-commonjs/e2etest/runWindows.test.js +60 -60
  42. package/lib-commonjs/e2etest/typesUpToDate.test.d.ts +6 -0
  43. package/lib-commonjs/e2etest/typesUpToDate.test.js +21 -0
  44. package/lib-commonjs/e2etest/typesUpToDate.test.js.map +1 -0
  45. package/lib-commonjs/generator-common/index.d.ts +39 -39
  46. package/lib-commonjs/generator-common/index.js +242 -242
  47. package/lib-commonjs/generator-windows/index.d.ts +10 -10
  48. package/lib-commonjs/generator-windows/index.js +316 -316
  49. package/lib-commonjs/index.d.ts +50 -50
  50. package/lib-commonjs/index.js +77 -77
  51. package/lib-commonjs/utils/build.d.ts +12 -12
  52. package/lib-commonjs/utils/build.js +84 -84
  53. package/lib-commonjs/utils/checkRequirements.d.ts +6 -6
  54. package/lib-commonjs/utils/checkRequirements.js +69 -69
  55. package/lib-commonjs/utils/commandWithProgress.d.ts +21 -21
  56. package/lib-commonjs/utils/commandWithProgress.js +149 -149
  57. package/lib-commonjs/utils/deploy.d.ts +12 -12
  58. package/lib-commonjs/utils/deploy.js +353 -353
  59. package/lib-commonjs/utils/info.d.ts +6 -6
  60. package/lib-commonjs/utils/info.js +28 -28
  61. package/lib-commonjs/utils/msbuildtools.d.ts +28 -28
  62. package/lib-commonjs/utils/msbuildtools.js +273 -273
  63. package/lib-commonjs/utils/msbuildtools.js.map +1 -1
  64. package/lib-commonjs/utils/pathHelpers.d.ts +9 -9
  65. package/lib-commonjs/utils/pathHelpers.js +36 -36
  66. package/lib-commonjs/utils/telemetryHelpers.d.ts +29 -29
  67. package/lib-commonjs/utils/telemetryHelpers.js +120 -120
  68. package/lib-commonjs/utils/version.d.ts +19 -19
  69. package/lib-commonjs/utils/version.js +109 -109
  70. package/lib-commonjs/utils/vsInstalls.d.ts +34 -34
  71. package/lib-commonjs/utils/vsInstalls.js +99 -99
  72. package/lib-commonjs/utils/vstools.d.ts +16 -16
  73. package/lib-commonjs/utils/vstools.js +189 -189
  74. package/lib-commonjs/utils/winappdeploytool.d.ts +24 -24
  75. package/lib-commonjs/utils/winappdeploytool.js +108 -108
  76. package/package.json +16 -16
@@ -1,39 +1,39 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- * Licensed under the MIT License.
4
- * @format
5
- */
6
- /**
7
- * Text to replace, + config options
8
- */
9
- export type Replacements = {
10
- useMustache?: boolean;
11
- regExpPatternsToRemove?: RegExp[];
12
- [key: string]: any;
13
- };
14
- /**
15
- * Get a source file and replace parts of its contents.
16
- * @param srcPath Path to the source file.
17
- * @param replacements e.g. {'TextToBeReplaced': 'Replacement'}
18
- * @return The contents of the file with the replacements applied.
19
- */
20
- export declare function resolveContents(srcPath: string, replacements: Replacements): string;
21
- type ContentChangedCallbackOption = 'identical' | 'changed' | 'new' | null;
22
- type ContentChangedCallback = (path: string, option: ContentChangedCallbackOption) => Promise<'keep' | 'overwrite'>;
23
- /**
24
- * Copy a file to given destination, replacing parts of its contents.
25
- * @param srcPath Path to a file to be copied.
26
- * @param destPath Destination path.
27
- * @param replacements: e.g. {'TextToBeReplaced': 'Replacement'}
28
- * @param contentChangedCallback
29
- * Used when upgrading projects. Based on if file contents would change
30
- * when being replaced, allows the caller to specify whether the file
31
- * should be replaced or not.
32
- * If null, files will be overwritten.
33
- * Function(path, 'identical' | 'changed' | 'new') => 'keep' | 'overwrite'
34
- */
35
- export declare function copyAndReplace(srcPath: string, destPath: string, replacements: Replacements, contentChangedCallback: ContentChangedCallback | null): Promise<void>;
36
- export declare function createDir(destPath: string): void;
37
- export declare function copyAndReplaceWithChangedCallback(srcPath: string, destRoot: string, relativeDestPath: string, replacements?: Record<string, string>, alwaysOverwrite?: boolean): Promise<void>;
38
- export declare function copyAndReplaceAll(srcPath: string, destPath: string, relativeDestDir: string, replacements: Replacements, alwaysOverwrite: boolean): Promise<void>;
39
- export {};
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+ /**
7
+ * Text to replace, + config options
8
+ */
9
+ export type Replacements = {
10
+ useMustache?: boolean;
11
+ regExpPatternsToRemove?: RegExp[];
12
+ [key: string]: any;
13
+ };
14
+ /**
15
+ * Get a source file and replace parts of its contents.
16
+ * @param srcPath Path to the source file.
17
+ * @param replacements e.g. {'TextToBeReplaced': 'Replacement'}
18
+ * @return The contents of the file with the replacements applied.
19
+ */
20
+ export declare function resolveContents(srcPath: string, replacements: Replacements): string;
21
+ type ContentChangedCallbackOption = 'identical' | 'changed' | 'new' | null;
22
+ type ContentChangedCallback = (path: string, option: ContentChangedCallbackOption) => Promise<'keep' | 'overwrite'>;
23
+ /**
24
+ * Copy a file to given destination, replacing parts of its contents.
25
+ * @param srcPath Path to a file to be copied.
26
+ * @param destPath Destination path.
27
+ * @param replacements: e.g. {'TextToBeReplaced': 'Replacement'}
28
+ * @param contentChangedCallback
29
+ * Used when upgrading projects. Based on if file contents would change
30
+ * when being replaced, allows the caller to specify whether the file
31
+ * should be replaced or not.
32
+ * If null, files will be overwritten.
33
+ * Function(path, 'identical' | 'changed' | 'new') => 'keep' | 'overwrite'
34
+ */
35
+ export declare function copyAndReplace(srcPath: string, destPath: string, replacements: Replacements, contentChangedCallback: ContentChangedCallback | null): Promise<void>;
36
+ export declare function createDir(destPath: string): void;
37
+ export declare function copyAndReplaceWithChangedCallback(srcPath: string, destRoot: string, relativeDestPath: string, replacements?: Record<string, string>, alwaysOverwrite?: boolean): Promise<void>;
38
+ export declare function copyAndReplaceAll(srcPath: string, destPath: string, relativeDestDir: string, replacements: Replacements, alwaysOverwrite: boolean): Promise<void>;
39
+ export {};
@@ -1,243 +1,243 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) Microsoft Corporation.
4
- * Licensed under the MIT License.
5
- * @format
6
- */
7
- var __importDefault = (this && this.__importDefault) || function (mod) {
8
- return (mod && mod.__esModule) ? mod : { "default": mod };
9
- };
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.copyAndReplaceAll = exports.copyAndReplaceWithChangedCallback = exports.createDir = exports.copyAndReplace = exports.resolveContents = void 0;
12
- const fs_1 = __importDefault(require("@react-native-windows/fs"));
13
- const chalk_1 = __importDefault(require("chalk"));
14
- const prompts_1 = __importDefault(require("prompts"));
15
- const path_1 = __importDefault(require("path"));
16
- const mustache_1 = __importDefault(require("mustache"));
17
- const telemetry_1 = require("@react-native-windows/telemetry");
18
- function walk(current) {
19
- if (!fs_1.default.lstatSync(current).isDirectory()) {
20
- return [current];
21
- }
22
- const files = fs_1.default
23
- .readdirSync(current)
24
- .map(child => walk(path_1.default.join(current, child)));
25
- const result = [];
26
- return result.concat.apply([current], files);
27
- }
28
- /**
29
- * Get a source file and replace parts of its contents.
30
- * @param srcPath Path to the source file.
31
- * @param replacements e.g. {'TextToBeReplaced': 'Replacement'}
32
- * @return The contents of the file with the replacements applied.
33
- */
34
- function resolveContents(srcPath, replacements) {
35
- let content = fs_1.default.readFileSync(srcPath, 'utf8');
36
- if (content.includes('\r\n')) {
37
- // CRLF file, make sure multiline replacements are also CRLF
38
- for (const key of Object.keys(replacements)) {
39
- if (typeof replacements[key] === 'string') {
40
- replacements[key] = replacements[key].replace(/(?<!\r)\n/g, '\r\n');
41
- }
42
- }
43
- }
44
- else {
45
- // LF file, make sure multiline replacements are also LF
46
- for (const key of Object.keys(replacements)) {
47
- if (typeof replacements[key] === 'string') {
48
- replacements[key] = replacements[key].replace(/\r\n/g, '\n');
49
- }
50
- }
51
- }
52
- if (replacements.useMustache) {
53
- content = mustache_1.default.render(content, replacements);
54
- (replacements.regExpPatternsToRemove || []).forEach(regexPattern => {
55
- content = content.replace(new RegExp(regexPattern, 'g'), '');
56
- });
57
- }
58
- else {
59
- Object.keys(replacements).forEach(regex => {
60
- content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
61
- });
62
- }
63
- return content;
64
- }
65
- exports.resolveContents = resolveContents;
66
- // Binary files, don't process these (avoid decoding as utf8)
67
- const binaryExtensions = ['.png', '.jar', '.keystore', '.ico', '.rc'];
68
- /**
69
- * Copy a file to given destination, replacing parts of its contents.
70
- * @param srcPath Path to a file to be copied.
71
- * @param destPath Destination path.
72
- * @param replacements: e.g. {'TextToBeReplaced': 'Replacement'}
73
- * @param contentChangedCallback
74
- * Used when upgrading projects. Based on if file contents would change
75
- * when being replaced, allows the caller to specify whether the file
76
- * should be replaced or not.
77
- * If null, files will be overwritten.
78
- * Function(path, 'identical' | 'changed' | 'new') => 'keep' | 'overwrite'
79
- */
80
- async function copyAndReplace(srcPath, destPath, replacements, contentChangedCallback) {
81
- if (fs_1.default.lstatSync(srcPath).isDirectory()) {
82
- if (!fs_1.default.existsSync(destPath)) {
83
- fs_1.default.mkdirSync(destPath);
84
- }
85
- // Not recursive
86
- return;
87
- }
88
- const extension = path_1.default.extname(srcPath);
89
- if (binaryExtensions.includes(extension)) {
90
- // Binary file
91
- let shouldOverwrite = 'overwrite';
92
- if (contentChangedCallback) {
93
- const newContentBuffer = fs_1.default.readFileSync(srcPath);
94
- let contentChanged = 'identical';
95
- try {
96
- const origContentBuffer = fs_1.default.readFileSync(destPath);
97
- if (Buffer.compare(origContentBuffer, newContentBuffer) !== 0) {
98
- contentChanged = 'changed';
99
- }
100
- }
101
- catch (err) {
102
- if (err.code === 'ENOENT') {
103
- contentChanged = 'new';
104
- }
105
- else {
106
- throw err;
107
- }
108
- }
109
- shouldOverwrite = await contentChangedCallback(destPath, contentChanged);
110
- }
111
- if (shouldOverwrite === 'overwrite') {
112
- copyBinaryFile(srcPath, destPath, err => {
113
- if (err) {
114
- throw err;
115
- }
116
- });
117
- }
118
- }
119
- else {
120
- // Text file
121
- const srcPermissions = fs_1.default.statSync(srcPath).mode;
122
- const content = resolveContents(srcPath, replacements);
123
- let shouldOverwrite = 'overwrite';
124
- if (contentChangedCallback) {
125
- // Check if contents changed and ask to overwrite
126
- let contentChanged = 'identical';
127
- try {
128
- const origContent = fs_1.default.readFileSync(destPath, 'utf8');
129
- if (content !== origContent) {
130
- // logger.info('Content changed: ' + destPath);
131
- contentChanged = 'changed';
132
- }
133
- }
134
- catch (err) {
135
- if (err.code === 'ENOENT') {
136
- contentChanged = 'new';
137
- }
138
- else {
139
- throw err;
140
- }
141
- }
142
- shouldOverwrite = await contentChangedCallback(destPath, contentChanged);
143
- }
144
- if (shouldOverwrite === 'overwrite') {
145
- fs_1.default.writeFileSync(destPath, content, {
146
- encoding: 'utf8',
147
- mode: srcPermissions,
148
- });
149
- }
150
- }
151
- }
152
- exports.copyAndReplace = copyAndReplace;
153
- /**
154
- * Same as 'cp' on Unix. Don't do any replacements.
155
- */
156
- function copyBinaryFile(srcPath, destPath, cb) {
157
- let cbCalled = false;
158
- const srcPermissions = fs_1.default.statSync(srcPath).mode;
159
- const readStream = fs_1.default.createReadStream(srcPath);
160
- readStream.on('error', err => {
161
- done(err);
162
- });
163
- const writeStream = fs_1.default.createWriteStream(destPath, {
164
- mode: srcPermissions,
165
- });
166
- writeStream.on('error', err => {
167
- done(err);
168
- });
169
- writeStream.on('close', () => {
170
- done();
171
- });
172
- readStream.pipe(writeStream);
173
- function done(err) {
174
- if (!cbCalled) {
175
- cb(err);
176
- cbCalled = true;
177
- }
178
- }
179
- }
180
- function createDir(destPath) {
181
- if (!fs_1.default.existsSync(destPath)) {
182
- fs_1.default.mkdirSync(destPath);
183
- }
184
- }
185
- exports.createDir = createDir;
186
- async function copyAndReplaceWithChangedCallback(srcPath, destRoot, relativeDestPath, replacements, alwaysOverwrite) {
187
- if (!replacements) {
188
- replacements = {};
189
- }
190
- const contentChangedCallback = alwaysOverwrite
191
- ? (_, contentChanged) => alwaysOverwriteContentChangedCallback(srcPath, relativeDestPath, contentChanged)
192
- : (_, contentChanged) => upgradeFileContentChangedCallback(srcPath, relativeDestPath, contentChanged);
193
- await copyAndReplace(srcPath, path_1.default.join(destRoot, relativeDestPath), replacements, contentChangedCallback);
194
- }
195
- exports.copyAndReplaceWithChangedCallback = copyAndReplaceWithChangedCallback;
196
- async function copyAndReplaceAll(srcPath, destPath, relativeDestDir, replacements, alwaysOverwrite) {
197
- for (const absoluteSrcFilePath of walk(srcPath)) {
198
- const filename = path_1.default.relative(srcPath, absoluteSrcFilePath);
199
- const relativeDestPath = path_1.default.join(relativeDestDir, filename);
200
- await copyAndReplaceWithChangedCallback(absoluteSrcFilePath, destPath, relativeDestPath, replacements, alwaysOverwrite);
201
- }
202
- }
203
- exports.copyAndReplaceAll = copyAndReplaceAll;
204
- async function alwaysOverwriteContentChangedCallback(absoluteSrcFilePath, relativeDestPath, contentChanged) {
205
- if (contentChanged === 'new') {
206
- console.log(`${chalk_1.default.bold('new')} ${relativeDestPath}`);
207
- return 'overwrite';
208
- }
209
- if (contentChanged === 'changed') {
210
- console.log(`${chalk_1.default.bold('changed')} ${relativeDestPath} ${chalk_1.default.yellow('[overwriting]')}`);
211
- return 'overwrite';
212
- }
213
- if (contentChanged === 'identical') {
214
- return 'keep';
215
- }
216
- throw new telemetry_1.CodedError('Autolinking', `Unknown file changed state: ${relativeDestPath}, ${contentChanged}`);
217
- }
218
- async function upgradeFileContentChangedCallback(absoluteSrcFilePath, relativeDestPath, contentChanged) {
219
- if (contentChanged === 'new') {
220
- console.log(`${chalk_1.default.bold('new')} ${relativeDestPath}`);
221
- return 'overwrite';
222
- }
223
- if (contentChanged === 'changed') {
224
- console.log(`${chalk_1.default.bold(relativeDestPath)} ` +
225
- `has changed in the new version.\nDo you want to keep your ${relativeDestPath} or replace it with the ` +
226
- 'latest version?\nMake sure you have any changes you made to this file saved somewhere.\n' +
227
- `You can see the new version here: ${absoluteSrcFilePath}`);
228
- const { shouldReplace } = await (0, prompts_1.default)([
229
- {
230
- name: 'shouldReplace',
231
- type: 'confirm',
232
- message: `Do you want to replace ${relativeDestPath}?`,
233
- initial: false,
234
- },
235
- ]);
236
- return shouldReplace ? 'overwrite' : 'keep';
237
- }
238
- if (contentChanged === 'identical') {
239
- return 'keep';
240
- }
241
- throw new telemetry_1.CodedError('Autolinking', `Unknown file changed state: ${relativeDestPath}, ${contentChanged}`);
242
- }
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation.
4
+ * Licensed under the MIT License.
5
+ * @format
6
+ */
7
+ var __importDefault = (this && this.__importDefault) || function (mod) {
8
+ return (mod && mod.__esModule) ? mod : { "default": mod };
9
+ };
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.copyAndReplaceAll = exports.copyAndReplaceWithChangedCallback = exports.createDir = exports.copyAndReplace = exports.resolveContents = void 0;
12
+ const fs_1 = __importDefault(require("@react-native-windows/fs"));
13
+ const chalk_1 = __importDefault(require("chalk"));
14
+ const prompts_1 = __importDefault(require("prompts"));
15
+ const path_1 = __importDefault(require("path"));
16
+ const mustache_1 = __importDefault(require("mustache"));
17
+ const telemetry_1 = require("@react-native-windows/telemetry");
18
+ function walk(current) {
19
+ if (!fs_1.default.lstatSync(current).isDirectory()) {
20
+ return [current];
21
+ }
22
+ const files = fs_1.default
23
+ .readdirSync(current)
24
+ .map(child => walk(path_1.default.join(current, child)));
25
+ const result = [];
26
+ return result.concat.apply([current], files);
27
+ }
28
+ /**
29
+ * Get a source file and replace parts of its contents.
30
+ * @param srcPath Path to the source file.
31
+ * @param replacements e.g. {'TextToBeReplaced': 'Replacement'}
32
+ * @return The contents of the file with the replacements applied.
33
+ */
34
+ function resolveContents(srcPath, replacements) {
35
+ let content = fs_1.default.readFileSync(srcPath, 'utf8');
36
+ if (content.includes('\r\n')) {
37
+ // CRLF file, make sure multiline replacements are also CRLF
38
+ for (const key of Object.keys(replacements)) {
39
+ if (typeof replacements[key] === 'string') {
40
+ replacements[key] = replacements[key].replace(/(?<!\r)\n/g, '\r\n');
41
+ }
42
+ }
43
+ }
44
+ else {
45
+ // LF file, make sure multiline replacements are also LF
46
+ for (const key of Object.keys(replacements)) {
47
+ if (typeof replacements[key] === 'string') {
48
+ replacements[key] = replacements[key].replace(/\r\n/g, '\n');
49
+ }
50
+ }
51
+ }
52
+ if (replacements.useMustache) {
53
+ content = mustache_1.default.render(content, replacements);
54
+ (replacements.regExpPatternsToRemove || []).forEach(regexPattern => {
55
+ content = content.replace(new RegExp(regexPattern, 'g'), '');
56
+ });
57
+ }
58
+ else {
59
+ Object.keys(replacements).forEach(regex => {
60
+ content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
61
+ });
62
+ }
63
+ return content;
64
+ }
65
+ exports.resolveContents = resolveContents;
66
+ // Binary files, don't process these (avoid decoding as utf8)
67
+ const binaryExtensions = ['.png', '.jar', '.keystore', '.ico', '.rc'];
68
+ /**
69
+ * Copy a file to given destination, replacing parts of its contents.
70
+ * @param srcPath Path to a file to be copied.
71
+ * @param destPath Destination path.
72
+ * @param replacements: e.g. {'TextToBeReplaced': 'Replacement'}
73
+ * @param contentChangedCallback
74
+ * Used when upgrading projects. Based on if file contents would change
75
+ * when being replaced, allows the caller to specify whether the file
76
+ * should be replaced or not.
77
+ * If null, files will be overwritten.
78
+ * Function(path, 'identical' | 'changed' | 'new') => 'keep' | 'overwrite'
79
+ */
80
+ async function copyAndReplace(srcPath, destPath, replacements, contentChangedCallback) {
81
+ if (fs_1.default.lstatSync(srcPath).isDirectory()) {
82
+ if (!fs_1.default.existsSync(destPath)) {
83
+ fs_1.default.mkdirSync(destPath);
84
+ }
85
+ // Not recursive
86
+ return;
87
+ }
88
+ const extension = path_1.default.extname(srcPath);
89
+ if (binaryExtensions.includes(extension)) {
90
+ // Binary file
91
+ let shouldOverwrite = 'overwrite';
92
+ if (contentChangedCallback) {
93
+ const newContentBuffer = fs_1.default.readFileSync(srcPath);
94
+ let contentChanged = 'identical';
95
+ try {
96
+ const origContentBuffer = fs_1.default.readFileSync(destPath);
97
+ if (Buffer.compare(origContentBuffer, newContentBuffer) !== 0) {
98
+ contentChanged = 'changed';
99
+ }
100
+ }
101
+ catch (err) {
102
+ if (err.code === 'ENOENT') {
103
+ contentChanged = 'new';
104
+ }
105
+ else {
106
+ throw err;
107
+ }
108
+ }
109
+ shouldOverwrite = await contentChangedCallback(destPath, contentChanged);
110
+ }
111
+ if (shouldOverwrite === 'overwrite') {
112
+ copyBinaryFile(srcPath, destPath, err => {
113
+ if (err) {
114
+ throw err;
115
+ }
116
+ });
117
+ }
118
+ }
119
+ else {
120
+ // Text file
121
+ const srcPermissions = fs_1.default.statSync(srcPath).mode;
122
+ const content = resolveContents(srcPath, replacements);
123
+ let shouldOverwrite = 'overwrite';
124
+ if (contentChangedCallback) {
125
+ // Check if contents changed and ask to overwrite
126
+ let contentChanged = 'identical';
127
+ try {
128
+ const origContent = fs_1.default.readFileSync(destPath, 'utf8');
129
+ if (content !== origContent) {
130
+ // logger.info('Content changed: ' + destPath);
131
+ contentChanged = 'changed';
132
+ }
133
+ }
134
+ catch (err) {
135
+ if (err.code === 'ENOENT') {
136
+ contentChanged = 'new';
137
+ }
138
+ else {
139
+ throw err;
140
+ }
141
+ }
142
+ shouldOverwrite = await contentChangedCallback(destPath, contentChanged);
143
+ }
144
+ if (shouldOverwrite === 'overwrite') {
145
+ fs_1.default.writeFileSync(destPath, content, {
146
+ encoding: 'utf8',
147
+ mode: srcPermissions,
148
+ });
149
+ }
150
+ }
151
+ }
152
+ exports.copyAndReplace = copyAndReplace;
153
+ /**
154
+ * Same as 'cp' on Unix. Don't do any replacements.
155
+ */
156
+ function copyBinaryFile(srcPath, destPath, cb) {
157
+ let cbCalled = false;
158
+ const srcPermissions = fs_1.default.statSync(srcPath).mode;
159
+ const readStream = fs_1.default.createReadStream(srcPath);
160
+ readStream.on('error', err => {
161
+ done(err);
162
+ });
163
+ const writeStream = fs_1.default.createWriteStream(destPath, {
164
+ mode: srcPermissions,
165
+ });
166
+ writeStream.on('error', err => {
167
+ done(err);
168
+ });
169
+ writeStream.on('close', () => {
170
+ done();
171
+ });
172
+ readStream.pipe(writeStream);
173
+ function done(err) {
174
+ if (!cbCalled) {
175
+ cb(err);
176
+ cbCalled = true;
177
+ }
178
+ }
179
+ }
180
+ function createDir(destPath) {
181
+ if (!fs_1.default.existsSync(destPath)) {
182
+ fs_1.default.mkdirSync(destPath);
183
+ }
184
+ }
185
+ exports.createDir = createDir;
186
+ async function copyAndReplaceWithChangedCallback(srcPath, destRoot, relativeDestPath, replacements, alwaysOverwrite) {
187
+ if (!replacements) {
188
+ replacements = {};
189
+ }
190
+ const contentChangedCallback = alwaysOverwrite
191
+ ? (_, contentChanged) => alwaysOverwriteContentChangedCallback(srcPath, relativeDestPath, contentChanged)
192
+ : (_, contentChanged) => upgradeFileContentChangedCallback(srcPath, relativeDestPath, contentChanged);
193
+ await copyAndReplace(srcPath, path_1.default.join(destRoot, relativeDestPath), replacements, contentChangedCallback);
194
+ }
195
+ exports.copyAndReplaceWithChangedCallback = copyAndReplaceWithChangedCallback;
196
+ async function copyAndReplaceAll(srcPath, destPath, relativeDestDir, replacements, alwaysOverwrite) {
197
+ for (const absoluteSrcFilePath of walk(srcPath)) {
198
+ const filename = path_1.default.relative(srcPath, absoluteSrcFilePath);
199
+ const relativeDestPath = path_1.default.join(relativeDestDir, filename);
200
+ await copyAndReplaceWithChangedCallback(absoluteSrcFilePath, destPath, relativeDestPath, replacements, alwaysOverwrite);
201
+ }
202
+ }
203
+ exports.copyAndReplaceAll = copyAndReplaceAll;
204
+ async function alwaysOverwriteContentChangedCallback(absoluteSrcFilePath, relativeDestPath, contentChanged) {
205
+ if (contentChanged === 'new') {
206
+ console.log(`${chalk_1.default.bold('new')} ${relativeDestPath}`);
207
+ return 'overwrite';
208
+ }
209
+ if (contentChanged === 'changed') {
210
+ console.log(`${chalk_1.default.bold('changed')} ${relativeDestPath} ${chalk_1.default.yellow('[overwriting]')}`);
211
+ return 'overwrite';
212
+ }
213
+ if (contentChanged === 'identical') {
214
+ return 'keep';
215
+ }
216
+ throw new telemetry_1.CodedError('Autolinking', `Unknown file changed state: ${relativeDestPath}, ${contentChanged}`);
217
+ }
218
+ async function upgradeFileContentChangedCallback(absoluteSrcFilePath, relativeDestPath, contentChanged) {
219
+ if (contentChanged === 'new') {
220
+ console.log(`${chalk_1.default.bold('new')} ${relativeDestPath}`);
221
+ return 'overwrite';
222
+ }
223
+ if (contentChanged === 'changed') {
224
+ console.log(`${chalk_1.default.bold(relativeDestPath)} ` +
225
+ `has changed in the new version.\nDo you want to keep your ${relativeDestPath} or replace it with the ` +
226
+ 'latest version?\nMake sure you have any changes you made to this file saved somewhere.\n' +
227
+ `You can see the new version here: ${absoluteSrcFilePath}`);
228
+ const { shouldReplace } = await (0, prompts_1.default)([
229
+ {
230
+ name: 'shouldReplace',
231
+ type: 'confirm',
232
+ message: `Do you want to replace ${relativeDestPath}?`,
233
+ initial: false,
234
+ },
235
+ ]);
236
+ return shouldReplace ? 'overwrite' : 'keep';
237
+ }
238
+ if (contentChanged === 'identical') {
239
+ return 'keep';
240
+ }
241
+ throw new telemetry_1.CodedError('Autolinking', `Unknown file changed state: ${relativeDestPath}, ${contentChanged}`);
242
+ }
243
243
  //# sourceMappingURL=index.js.map
@@ -1,10 +1,10 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- * Licensed under the MIT License.
4
- * @format
5
- */
6
- import { GenerateOptions } from '..';
7
- export declare function copyProjectTemplateAndReplace(srcRootPath: string, destPath: string, newProjectName: string, namespace: string, options: GenerateOptions): Promise<void>;
8
- export declare function installScriptsAndDependencies(options: {
9
- verbose: boolean;
10
- }): Promise<void>;
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+ import { GenerateOptions } from '..';
7
+ export declare function copyProjectTemplateAndReplace(srcRootPath: string, destPath: string, newProjectName: string, namespace: string, options: GenerateOptions): Promise<void>;
8
+ export declare function installScriptsAndDependencies(options: {
9
+ verbose: boolean;
10
+ }): Promise<void>;