Skip to content

Commit 65c7c08

Browse files
committed
fix(build): resolve an explicit relative backupFolder against root
The previous root fix only pre-resolved backupFolder's *default* value against root; an explicitly-provided relative backupFolder (e.g. plugin.ts's syncBackupsDir, which is always a defined string) stayed relative. checkMeta()/saveBackup() then resolved it via a bare path.resolve(this.backupFolder, ...), falling back to process.cwd(). Resolve it against root unconditionally in the constructor instead.
1 parent 36dcffa commit 65c7c08

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/lib/dist.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class DistService {
132132

133133
const {
134134
root = process.cwd(),
135-
backupFolder = path.resolve(root, 'backups'),
135+
backupFolder = 'backups',
136136
distZipFolder = path.resolve(root, 'dist-zip'),
137137
distZipFilename = createDefaultZipFileName(this.pageName),
138138
backupNameTemplate = `{${TEMPLATE_PART_PAGE_NAME}}-{${TEMPLATE_PART_DATE}}.zip`,
@@ -143,7 +143,10 @@ export class DistService {
143143
} = syncOptions || {};
144144

145145
this.root = root;
146-
this.backupFolder = backupFolder;
146+
// Resolve eagerly (not just at the default) — checkMeta()/saveBackup() use this value
147+
// directly without prepending root, so an explicit relative backupFolder must be
148+
// anchored here or it silently falls back to process.cwd() at those call sites.
149+
this.backupFolder = path.resolve(root, backupFolder);
147150
this.backupNameTemplate = backupNameTemplate;
148151
this.dateFormat = dateFormat;
149152

tests/unit/lib/dist.service.root.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ describe('DistService — root-scoped path resolution', () => {
7676
expect(readBack?.toString('utf-8')).toBe('{"a":1}');
7777
});
7878

79+
it('resolves an explicit relative backupFolder against the explicit root, not process.cwd()', async () => {
80+
// eslint-disable-next-line no-new -- the constructor's fire-and-forget syncMeta() creates the folder
81+
new DistService('test-app', {
82+
root: projectRoot,
83+
backupFolder: 'backups',
84+
});
85+
86+
// Don't call checkMeta() ourselves: the constructor already kicked one off in the
87+
// background (unawaited), and a second concurrent call races its mkdir. Poll instead.
88+
await vi.waitFor(() => {
89+
expect(fs.existsSync(path.join(projectRoot, 'backups'))).toBe(true);
90+
});
91+
92+
expect(fs.existsSync(path.join(unrelatedCwd, 'backups'))).toBe(false);
93+
});
94+
7995
it('defaults root to process.cwd() when not provided', async () => {
8096
const service = new DistService('test-app', {});
8197

0 commit comments

Comments
 (0)