Skip to content

Commit

Permalink
feat: new sync command
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Nov 12, 2024
1 parent d76504e commit 85f27ab
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ body:
id: logs
attributes:
label: Logs
description: "Please include browser console and server logs around the time this bug occurred. Please try not to insert an image but copy paste the log text."
description: 'Please include browser console and server logs around the time this bug occurred. Please try not to insert an image but copy paste the log text.'
render: Shell
- type: dropdown
id: severity
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "Feature Request"
name: 'Feature Request'
description: Suggest an idea for this project
body:
- type: markdown
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ $ pnpm preview
- `tnf dev`: Start the development server.
- `tnf generate/g <type> <name>`: Generate a new page (or component and other types in the future).
- `tnf preview`: Preview the product after building the project.
- `tnf sync`: Sync the project to the temporary directory.

## API

Expand Down
10 changes: 5 additions & 5 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chokidar from 'chokidar';
import path from 'pathe';
import type { Config } from './config';
import { FRAMEWORK_NAME } from './constants';
import { prepare } from './prepare';
import { sync } from './sync';

export async function build({
cwd,
Expand All @@ -18,15 +18,15 @@ export async function build({
}) {
const tmpPath = path.join(cwd, `src/.${FRAMEWORK_NAME}`);

const doPrepare = async () => {
await prepare({
const doSync = async () => {
await sync({
cwd,
tmpPath,
config,
});
};

await doPrepare();
await doSync();

if (watch) {
const pagesDir = path.join(cwd, 'src/pages');
Expand All @@ -36,7 +36,7 @@ export async function build({
})
.on('all', async (event, path) => {
console.log(`File ${path} has been ${event}`);
await doPrepare();
await doSync();
});
}

Expand Down
9 changes: 9 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert';
import path from 'pathe';
import yargsParser from 'yargs-parser';
import { loadConfig } from './config.js';
import { FRAMEWORK_NAME, MIN_NODE_VERSION } from './constants.js';
Expand Down Expand Up @@ -50,6 +51,14 @@ async function run(cwd: string) {
type,
name,
});
case 'sync':
const { sync } = await import('./sync.js');
const tmpPath = path.join(cwd, `src/.${FRAMEWORK_NAME}`);
return sync({
cwd,
tmpPath,
config: await loadConfig({ cwd }),
});
default:
throw new Error(`Unknown command: ${cmd}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/prepare.ts → src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ interface BaseOptions {
config?: TnfConfig;
}

interface PrepareOptions extends BaseOptions {}
interface SyncOptions extends BaseOptions {}

export async function prepare(opts: PrepareOptions) {
export async function sync(opts: SyncOptions) {
const { cwd, tmpPath, config } = opts;

fs.rmSync(tmpPath, { recursive: true, force: true });
Expand Down
6 changes: 3 additions & 3 deletions templates/simple/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "myapp",
"private": true,
"scripts": {
"build": "tnf build",
"dev": "tnf dev",
"preview": "tnf preview"
"build": "tnf build",
"preview": "tnf preview",
"postinstall": "tnf sync"
},
"dependencies": {
"@types/react": "^18.3.12",
Expand Down

0 comments on commit 85f27ab

Please sign in to comment.