Skip to content

Commit 7f2be1c

Browse files
committed
Move from cp -r to fs-extras
1 parent 89f757b commit 7f2be1c

File tree

9 files changed

+123
-290
lines changed

9 files changed

+123
-290
lines changed

package-lock.json

Lines changed: 83 additions & 256 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@
4949
],
5050
"license": "Apache-2.0",
5151
"dependencies": {
52+
"fs-extra": "^10.1.0",
53+
"jsonc-parser": "^3.0.0",
5254
"tslib": "^2.3.1"
5355
},
5456
"devDependencies": {
5557
"@angular-devkit/architect": "^0.1303.3",
5658
"@angular-devkit/core": "^13.3.3",
5759
"@nuxt/types": "^2.15.8",
5860
"@types/cookie": "^0.5.1",
61+
"@types/fs-extra": "^9.0.13",
5962
"@types/inquirer": "^8.1.3",
6063
"@types/lru-cache": "^7.6.1",
6164
"@types/node": "^16.11.12",

src/frameworks/angular/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { WorkspaceNodeModulesArchitectHost } from '@angular-devkit/architect/nod
44
import { Target, Architect, targetFromTargetString, targetStringFromTarget } from '@angular-devkit/architect';
55
import { mkdir } from 'fs/promises';
66
import { join } from 'path';
7+
import { copy } from 'fs-extra';
8+
import { parse } from 'jsonc-parser';
79

8-
import { DeployConfig, exec, findDependency, PathFactory, spawn } from '../../utils';
10+
import { DeployConfig, findDependency, PathFactory, spawn } from '../../utils';
911

1012
export const build = async (config: DeployConfig | Required<DeployConfig>, getProjectPath: PathFactory) => {
1113

@@ -24,7 +26,7 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
2426
let prerenderTarget: Target|undefined;
2527

2628
if (!project) {
27-
const angularJson = JSON.parse(await host.readFile('angular.json'));
29+
const angularJson = parse(await host.readFile('angular.json'));
2830
project = angularJson.defaultProject;
2931
if (!project) throw `angular.json missing defaultProject`;
3032
}
@@ -103,7 +105,7 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
103105
if (typeof browserTargetOptions?.outputPath !== 'string') throw 'foo';
104106
const browserOutputPath = browserTargetOptions.outputPath;
105107
await mkdir(getHostingPath(), { recursive: true });
106-
await exec(`cp -r ${getProjectPath(browserOutputPath)}/* ${getHostingPath()}`);
108+
await copy(getProjectPath(browserOutputPath), getHostingPath());
107109

108110
const usingCloudFunctions = !!serverTarget;
109111

@@ -115,8 +117,8 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
115117
const serverOutputPath = serverTargetOptions.outputPath;
116118
await mkdir(deployPath('functions', serverOutputPath), { recursive: true });
117119
await mkdir(deployPath('functions', browserOutputPath), { recursive: true });
118-
await exec(`cp -r ${getProjectPath(serverOutputPath)}/* ${deployPath('functions', serverOutputPath)}`);
119-
await exec(`cp -r ${getProjectPath(browserOutputPath)}/* ${deployPath('functions', browserOutputPath)}`);
120+
await copy(getProjectPath(serverOutputPath), deployPath('functions', serverOutputPath));
121+
await copy(getProjectPath(browserOutputPath), deployPath('functions', browserOutputPath));
120122
bootstrapScript = `exports.handle = require('./${serverOutputPath}/main.js').app();\n`;
121123
const bundleDependencies = serverTargetOptions.bundleDependencies ?? true;
122124
if (bundleDependencies) {

src/frameworks/express/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import { readFile, mkdir } from 'fs/promises';
1616
import { join } from 'path';
17+
import { copy } from 'fs-extra';
1718

1819
import { DeployConfig, PathFactory, exec, spawn } from '../../utils';
1920

@@ -24,7 +25,7 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
2425

2526
if (packageJson.scripts?.build) {
2627
// TODO add to the firebaseTools logs
27-
await spawn('npm', ['--prefix', getProjectPath(), 'run', 'build'], {}, stdoutChunk => {
28+
await spawn('npm', ['run', 'build'], { cwd: getProjectPath() }, stdoutChunk => {
2829
console.log(stdoutChunk.toString());
2930
}, errChunk => {
3031
console.error(errChunk.toString());
@@ -99,7 +100,7 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
99100
await mkdir(getHostingPath(), { recursive: true });
100101

101102
if (packageJson.directories?.serve) {
102-
await exec(`cp -r ${getProjectPath(packageJson.directories.serve, '*')} ${deployPath('hosting')}`);
103+
await copy(getProjectPath(packageJson.directories.serve), deployPath('hosting'));
103104
}
104105

105106
if (serverRenderMethod) {

src/frameworks/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { existsSync, promises as fs } from 'fs';
1+
import { existsSync } from 'fs';
2+
import { rm } from 'fs/promises';
23

34
import { DeployConfig, PathFactory } from '../utils';
45

5-
const { rm } = fs;
6-
76
const dynamicImport = (getProjectPath: PathFactory) => {
87
const exists = (...files: string[]) => files.some(file => existsSync(getProjectPath(file)));
98
if (exists('next.config.js')) return import('./next.js/index.js');

src/frameworks/next.js/index.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { readFile, mkdir, copyFile } from 'fs/promises';
16-
import { dirname, join, relative } from 'path';
15+
import { readFile, mkdir, copyFile, stat } from 'fs/promises';
16+
import { dirname, extname, join } from 'path';
1717
import type { NextConfig } from 'next/dist/server/config-shared';
1818
import type { Header, Rewrite, Redirect } from 'next/dist/lib/load-custom-routes';
1919
import nextBuild from 'next/dist/build';
20+
import { copy } from 'fs-extra';
2021

2122
import { DeployConfig, PathFactory, exec } from '../../utils';
2223

@@ -50,21 +51,19 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
5051
const exportDetailJson = await readFile(getProjectPath(distDir, 'export-detail.json')).then(it => JSON.parse(it.toString()), () => { success: false });
5152
if (exportDetailJson.success) {
5253
usingCloudFunctions = false;
53-
asyncSteps.push(exec(`cp -r ${exportDetailJson.outDirectory}/* ${getHostingPath()}`));
54+
asyncSteps.push(
55+
copy(exportDetailJson.outDirectory, getHostingPath())
56+
);
5457
} else {
55-
await exec(`cp -r ${getProjectPath('public')}/* ${getHostingPath()}`);
56-
await exec(`cp -r ${getProjectPath(distDir, 'static')} ${getHostingPath('_next')}`);
58+
await copy(getProjectPath('public'), getHostingPath());
59+
await copy(getProjectPath(distDir, 'static'), getHostingPath('_next', 'static'));
5760

58-
// TODO clean this up, probably conflicts with the code blow
5961
const serverPagesDir = getProjectPath(distDir, 'server', 'pages');
60-
const htmlFiles = (await exec(`find ${serverPagesDir} -name '*.html'`) as string).split("\n").map(it => it.trim());
61-
await Promise.all(
62-
htmlFiles.map(async path => {
63-
const newPath = getHostingPath(relative(serverPagesDir, path));
64-
await mkdir(dirname(newPath), { recursive: true });
65-
await copyFile(path, newPath);
66-
})
67-
);
62+
await copy(serverPagesDir, getHostingPath(), { filter: async filename => {
63+
const status = await stat(filename);
64+
if (status.isDirectory()) return true;
65+
return extname(filename) === '.html'
66+
}});
6867

6968
const prerenderManifestBuffer = await readFile(getProjectPath(distDir, 'prerender-manifest.json'));
7069
const prerenderManifest = JSON.parse(prerenderManifestBuffer.toString());
@@ -96,8 +95,8 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
9695
await mkdir(deployPath('functions'), { recursive: true });
9796
asyncSteps.push(
9897
copyFile(getProjectPath('next.config.js'), deployPath('functions', 'next.config.js')),
99-
exec(`cp -r ${getProjectPath('public')} ${deployPath('functions', 'public')}`),
100-
exec(`cp -r ${getProjectPath(distDir)} ${deployPath('functions', distDir)}`),
98+
copy(getProjectPath('public'), deployPath('functions', 'public')),
99+
copy(getProjectPath(distDir), deployPath('functions', distDir)),
101100
);
102101
}
103102

src/frameworks/nuxt/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
import { readFile, mkdir } from 'fs/promises'
1616
import { join } from 'path';
17+
import { copy } from 'fs-extra';
1718

18-
import { DeployConfig, PathFactory, exec } from '../../utils';
19+
import { DeployConfig, PathFactory } from '../../utils';
1920
import { build as buildNuxt3 } from '../nuxt3';
2021

2122
export const build = async (config: DeployConfig | Required<DeployConfig>, getProjectPath: PathFactory) => {
@@ -49,17 +50,17 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
4950
const builder = await getBuilder(nuxtApp);
5051
const generator = new Generator(nuxtApp, builder);
5152
await generator.generate({ build: false, init: true });
52-
await exec(`cp -r ${join(generator.distPath, '*')} ${deployPath('hosting')}`);
53+
await copy(generator.distPath, deployPath('hosting'));
5354
await nuxtApp.server.close();
5455
usingCloudFunctions = !generator.isFullStatic;
5556
} else {
56-
await exec(`cp -r ${join(buildDir, 'dist', 'client', '*')} ${deployPath('hosting', assetsPath)}`);
57-
await exec(`cp -r ${getProjectPath(staticDir, '*')} ${deployPath('hosting')}`);
57+
await copy(join(buildDir, 'dist', 'client'), deployPath('hosting', assetsPath));
58+
await copy(getProjectPath(staticDir), deployPath('hosting'));
5859
}
5960

6061
if (usingCloudFunctions) {
6162
await mkdir(deployPath('functions'), { recursive: true });
62-
await exec(`cp -r ${buildDir} ${deployPath('functions')}`);
63+
await copy(buildDir, deployPath('functions'));
6364
}
6465

6566
const packageJsonBuffer = await readFile(getProjectPath('package.json'));

src/frameworks/nuxt3/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import { existsSync } from 'fs';
1616
import { readFile, mkdir, readdir } from 'fs/promises';
1717
import { join } from 'path';
18+
import { copy } from 'fs-extra';
1819

1920
import { DeployConfig, PathFactory, exec } from '../../utils';
2021

@@ -43,9 +44,9 @@ export const build = async (config: DeployConfig | Required<DeployConfig>, getPr
4344
await mkdir(getHostingPath(buildAssetsDir), { recursive: true });
4445

4546
if (usingCloudFunctions) {
46-
await exec(`cp -r ${getProjectPath(distDir, 'server', '*')} ${deployPath('functions')}`);
47+
await copy(getProjectPath(distDir, 'server'), deployPath('functions'));
4748
}
48-
await exec(`cp -r ${getProjectPath(distDir, 'public', '*')} ${deployPath('hosting')}`);
49+
await copy(getProjectPath(distDir, 'public'), deployPath('hosting'));
4950

5051
const packageJsonBuffer = await readFile(getProjectPath('package.json'));
5152
const packageJson = JSON.parse(packageJsonBuffer.toString());

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { exit } from 'process';
1717
import { writeFile, copyFile, stat } from 'fs/promises';
1818

1919
import { build } from './frameworks';
20-
import { defaultFirebaseToolsOptions, DEFAULT_REGION, exec, shortSiteName, spawn } from './utils';
20+
import { defaultFirebaseToolsOptions, DEFAULT_REGION, shortSiteName } from './utils';
2121
import { getFirebaseTools, normalizedHostingConfigs, getInquirer, needProjectId } from './firebase';
2222
import { spawnSync } from 'child_process';
2323

0 commit comments

Comments
 (0)