Skip to content

Commit

Permalink
refactor: refactor next-standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
tianyingchun committed Aug 1, 2024
1 parent 851a862 commit 67539ad
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 205 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-toys-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperse/hyper-env": patch
---

refactor `next-standalone`
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@
"@changesets/cli": "2.27.7",
"@commitlint/cli": "19.3.0",
"@commitlint/config-conventional": "19.2.2",
"@hyperse/eslint-config-hyperse": "^1.0.10",
"@hyperse/eslint-config-hyperse": "^1.0.12",
"@hyperse/exec-program": "^1.0.10",
"@types/minimist": "^1.2.5",
"@types/node": "^20.14.12",
"@types/node": "^22.0.2",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"commitizen": "4.3.0",
"cz-conventional-changelog": "3.3.0",
"eslint": "^9.8.0",
"husky": "9.1.3",
"husky": "9.1.4",
"lint-staged": "15.2.7",
"next": "14.2.5",
"npm-run-all": "^4.1.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"tsup": "^8.2.3",
"typescript": "^5.5.4",
"vitest": "^2.0.4"
"vitest": "^2.0.5"
},
"engines": {
"node": ">=18"
Expand Down
16 changes: 0 additions & 16 deletions src/file-walk.ts

This file was deleted.

57 changes: 38 additions & 19 deletions src/next-standalone.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import fs from 'fs';
import fsPromise from 'fs/promises';
import minimist from 'minimist';
import { dirname, resolve } from 'path';
import { dirname, isAbsolute, relative, resolve } from 'path';
import { nodeFileTrace } from '@vercel/nft';
import { fileWalk } from './file-walk.js';
import { getDirname } from './get-dir-name.js';
import { fileWalk } from './utils.js';

type Argv = {
f: string;
fromBase: string;
c: string;
copyToBase: string;
p: string;
/**
* THe project root folder, normally it always `process.cwd()`, it can be a relative path from the repository root folder.
* @example `docs` for `monorepo`
* @default `process.cwd()`
*/
projectCwd: string;

r: string;
/**
* The repository root folder
* @default `process.cwd()`
*/
repoCwd: string;
};

/**
Expand All @@ -21,41 +31,50 @@ type Argv = {
*/
export const nextStandalone = async (args: string[]) => {
const binFile = getDirname(import.meta.url, '../bin/hyper-env.mjs');
const defaultRepoCwd = /\/node_modules\//.test(binFile)
? binFile.split('node_modules')[0]
: process.cwd();

const defaultProjectCwd = process.cwd();

const argv = minimist<Argv>(args, {
'--': true,
alias: {
f: 'fromBase',
c: 'copyToBase',
r: 'repoCwd',
p: 'projectCwd',
},
default: {
fromBase: process.cwd(),
copyToBase: process.cwd(),
repoCwd: defaultRepoCwd,
projectCwd: defaultProjectCwd,
},
});

const { fromBase, copyToBase } = argv;
const repoCwd = resolve(defaultRepoCwd, argv.repoCwd);
const projectCwd = resolve(repoCwd, argv.projectCwd);

const { fileList } = await nodeFileTrace([binFile], {
base: fromBase,
base: repoCwd,
});

const envFiles = await fileWalk(['.env', '.env.*'], {
cwd: fromBase,
absolute: false,
cwd: projectCwd,
absolute: true,
});

for (const absEnvFile of envFiles) {
fileList.add(absEnvFile);
}

for (const filePath of fileList) {
const copyTo = resolve(copyToBase, '.next/standalone', filePath);
const copyFrom = resolve(repoCwd, filePath);
const copyTo = resolve(
projectCwd,
'.next/standalone',
isAbsolute(filePath) ? relative(projectCwd, filePath) : filePath
);
if (!fs.existsSync(dirname(copyTo))) {
fs.mkdirSync(dirname(copyTo), { recursive: true });
}
await fsPromise.copyFile(
resolve(fromBase, filePath),
resolve(copyToBase, '.next/standalone', filePath)
);
await fsPromise.copyFile(copyFrom, copyTo);
}
};
33 changes: 33 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Options } from 'globby';
import { globby } from 'globby';
import { existsSync } from 'node:fs';
import { join } from 'node:path';

export const fileWalk = (
pattern: string | readonly string[],
options: Options = {}
): Promise<string[]> => {
const ignorePattern = options.ignore || [];
return globby(pattern, {
absolute: false,
dot: true,
unique: true,
...options,
ignore: [...ignorePattern, '**/__MACOSX/**', '**/*.DS_Store'],
});
};

/**
* A monorepo (mono repository) is a single repository that stores all of your code and assets for every project.
* @param cwd normally it always process.cwd()
*/
export const isMonorepo = async (cwd: string = process.cwd()) => {
const monoPackageCwd = join(cwd, 'packages');
if (existsSync(monoPackageCwd)) {
const packageJson = await fileWalk(join(monoPackageCwd, '*/package.json'), {
cwd,
});
return packageJson.length > 0;
}
return false;
};
29 changes: 12 additions & 17 deletions tests/next-standalone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ describe('Next Standalone', () => {
});

it('should copy only the necessary files with specificed correct parameters', async () => {
const fromBase = process.cwd();
const copyToBase = process.cwd();
await nextStandalone(['--fromBase', fromBase, '--copyToBase', copyToBase]);
const repoCwd = process.cwd();
const projectCwd = process.cwd();
await nextStandalone(['--repoCwd', repoCwd, '--projectCwd', projectCwd]);

const fileList = [
'bin/hyper-env.mjs',
Expand All @@ -92,12 +92,7 @@ describe('Next Standalone', () => {
});

it('should correct handle copy .env files for workdir', async () => {
await nextStandalone([
'--fromBase',
fixtureCwd,
'--copyToBase',
fixtureCwd,
]);
await nextStandalone(['--repoCwd', fixtureCwd, '--projectCwd', fixtureCwd]);
for (const envFile of Object.keys(envFiles)) {
expect(fsPromise.copyFile).toHaveBeenCalledWith(
join(fixtureCwd, envFile),
Expand All @@ -107,23 +102,23 @@ describe('Next Standalone', () => {
});

it('should correct handle argv dummy standard parameters', async () => {
const fromBase = '/fromBase';
const copyToBase = '/copyToBase';
await nextStandalone(['--fromBase', fromBase, '--copyToBase', copyToBase]);
const repoCwd = '/fromBase';
const projectCwd = '/copyToBase';
await nextStandalone(['--repoCwd', repoCwd, '--projectCwd', projectCwd]);
[
[binFile, `${copyToBase}/.next${process.cwd()}/bin/hyper-env.mjs`],
[binFile, `${projectCwd}/.next${process.cwd()}/bin/hyper-env.mjs`],
].forEach(([from, to]) => {
expect(fsPromise.copyFile).toHaveBeenCalledWith(from, to);
});
expect(fsPromise.copyFile).toHaveBeenCalledTimes(1);
});

it('should correct handle argv dummy standard parameters with alias', async () => {
const fromBase = '/fromBase';
const copyToBase = '/copyToBase';
await nextStandalone(['-f', fromBase, '-c', copyToBase]);
const repoCwd = '/fromBase';
const projectCwd = '/copyToBase';
await nextStandalone(['-r', repoCwd, '-p', projectCwd]);
[
[binFile, `${copyToBase}/.next${process.cwd()}/bin/hyper-env.mjs`],
[binFile, `${projectCwd}/.next${process.cwd()}/bin/hyper-env.mjs`],
].forEach(([from, to]) => {
expect(fsPromise.copyFile).toHaveBeenCalledWith(from, to);
});
Expand Down
Loading

0 comments on commit 67539ad

Please sign in to comment.