Skip to content

Commit ff11a6e

Browse files
Merge pull request #55 from contentstack/development
DX | 30-06-2025 | Release
2 parents b67c7b6 + 58e7931 commit ff11a6e

File tree

5 files changed

+345
-535
lines changed

5 files changed

+345
-535
lines changed

clean.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// scripts/clean.js
2+
const fs = require("fs");
3+
const path = require("path");
4+
const rimraf = require("rimraf");
5+
const config = require("./config/all");
6+
const { merge } = require("lodash");
7+
8+
// Set application's environment
9+
const env = process.env.NODE_ENV || config.environment || "development";
10+
11+
let envConfig;
12+
if (fs.existsSync(path.join(__dirname, "config", env + ".js"))) {
13+
envConfig = require(path.join(__dirname, "config", env));
14+
} else {
15+
envConfig = require(path.join(__dirname, "config", "development"));
16+
}
17+
18+
const appConfig = merge(config, envConfig);
19+
20+
const pathsToDelete = [".ledger", ".token", "unprocessible"];
21+
22+
// Preserve .checkpoint only if explicitly set in the configuration
23+
if (!appConfig.checkpoint?.preserve) {
24+
pathsToDelete.push(".checkpoint");
25+
}
26+
27+
pathsToDelete.forEach((fileOrDir) => {
28+
const fullPath = path.join(__dirname, fileOrDir);
29+
if (fs.existsSync(fullPath)) {
30+
rimraf.sync(fullPath);
31+
console.log("Deleted:", fileOrDir);
32+
} else {
33+
console.log("Not found (skipped):", fileOrDir);
34+
}
35+
});

config/all.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ const config = {
6969
},
7070
},
7171
],
72+
checkpoint: {
73+
enabled: false, // Set to true if you want to enable checkpoint
74+
filePath: ".checkpoint",
75+
preserve: false // Set to true if you want to preserve the checkpoint file during clean operation
76+
},
7277
}
7378

7479
module.exports = config

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ if (fs.existsSync(path.join(__dirname, "config", env + ".js"))) {
3232

3333
const appConfig = _.merge(config, envConfig);
3434

35+
// Optional checkpoint reading
36+
if (appConfig.checkpoint?.enabled) {
37+
const checkpointPath = path.join(__dirname, appConfig.checkpoint.filePath || ".checkpoint");
38+
const checkPoint = readHiddenFile(checkpointPath);
39+
if (checkPoint) {
40+
console.log("Found sync token in checkpoint file:", checkPoint);
41+
appConfig.contentstack.sync_token = checkPoint.token;
42+
console.log("Using sync token:", appConfig.contentstack.sync_token);
43+
}
44+
}
45+
3546
datasyncManager.setConfig(appConfig);
3647
datasyncManager.setAssetStore(assetStore);
3748
datasyncManager.setContentStore(contentStore);
@@ -45,3 +56,16 @@ datasyncManager
4556
.catch((error) => {
4657
console.error(error);
4758
});
59+
60+
function readHiddenFile(filePath) {
61+
try {
62+
if (!fs.existsSync(filePath)) {
63+
console.error("File does not exist:", filePath);
64+
return;
65+
}
66+
const data = fs.readFileSync(filePath, "utf8");
67+
return JSON.parse(data);
68+
} catch (err) {
69+
console.error("Error reading file:", err?.message);
70+
}
71+
}

0 commit comments

Comments
 (0)