WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
This repository was archived by the owner on Mar 14, 2022. It is now read-only.

Commit 4f5efd3

Browse files
committed
download db file to autofill
1 parent 913f93a commit 4f5efd3

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

cms/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
"version": "2.3.0",
44
"description": "Strapi supported CMS and Backend",
55
"scripts": {
6+
"postinstall": "node ./tools/post-install.js",
67
"dev": "strapi develop",
8+
"boot": "node ./tools/env-Decrypter.js && node ./tools/create-local-db.js",
79
"build": "strapi build --clean",
8-
"postinstall": "node ./tools/env-Decrypter.js",
910
"pm2": "pm2",
1011
"start": "pm2 start ecosystem.config.js --wait-ready",
1112
"poststart": "pm2 monit",
1213
"server:testing": "cross-env TESTING=TRUE strapi start",
1314
"test": "cross-env NODE_ENV=test jest --runInBand --forceExit",
1415
"testw": "npm run test -- --watch",
15-
"strapi": "strapi",
16-
"backup": "node ./tools/local-backup.js"
16+
"strapi": "strapi"
1717
},
1818
"devDependencies": {
1919
"@types/jest": "^26.0.23",

cms/tools/create-local-db.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const fs = require('fs');
2+
const https = require('https');
3+
4+
const FILE_LOCATION = './db/data.db';
5+
const DOWNLOAD_URL =
6+
'https://github.com/aditya-mitra/dsckiit-website-2.0/releases/download/v2.2/data.db';
7+
8+
function showProgress(current, totalFileSize, totalFileSizeInMB) {
9+
process.stdout.clearLine();
10+
process.stdout.cursorTo(0);
11+
const writeLine =
12+
'\x1b[33m\x1b[1m Downloading the file -- \x1b[43m\x1b[37m' +
13+
((100.0 * current) / totalFileSize).toFixed(2) +
14+
'% \x1b[0m\x1b[33m\x1b[1m (' +
15+
(current / 1048576).toFixed(2) +
16+
' MB) of total file size: ' +
17+
totalFileSizeInMB.toFixed(2) +
18+
' MB \x1b[0m';
19+
20+
process.stdout.write(writeLine);
21+
}
22+
23+
function createDBFolder() {
24+
const folderExists = fs.existsSync('./db');
25+
if (!folderExists) {
26+
console.log('\x1b[0m\x1b[1m\x1b[35m Creating db/ folder\x1b[0m');
27+
fs.mkdirSync('./db');
28+
}
29+
}
30+
31+
function showError(error) {
32+
console.log('\x1b[0m\x1b[1m\x1b[31m Failed to Download! \x1b[0m', error);
33+
}
34+
35+
/* discuss whether to take the versino from package.json
36+
function getVersionFromPackageJSON(){
37+
const fileContent = fs.readFileSync('./package.json',{encoding:'utf-8'})
38+
const packageJson = JSON.parse(fileContent);
39+
return packageJson.version;
40+
}
41+
*/
42+
43+
function getFile(url) {
44+
https.get(url, (response) => {
45+
if (response.statusCode === 301 || response.statusCode === 302) {
46+
return getFile(response.headers.location);
47+
} else if (response.statusCode >= 400 && response.statusCode < 600) {
48+
return showError(`StatusCode : ${response.statusCode}`);
49+
}
50+
const fileStream = fs.createWriteStream(FILE_LOCATION);
51+
const totalFileSize = parseInt(response.headers['content-length'], 10);
52+
let current = 0;
53+
const totalFileSizeInMB = totalFileSize / 1048576;
54+
55+
response.on('data', (chunk) => {
56+
current += chunk.length;
57+
showProgress(current, totalFileSize, totalFileSizeInMB);
58+
});
59+
60+
response.on('end', () => {
61+
console.log('\n\x1b[1m\x1b[32m Download complete!\x1b[0m');
62+
console.log(' \x1b[0m\x1b[1m\x1b[34mDatabase File saved to \x1b[4m./db/data.db\x1b[0m');
63+
});
64+
65+
response.on('error', (err) => {
66+
return showError(err);
67+
});
68+
69+
response.pipe(fileStream);
70+
});
71+
}
72+
73+
function run() {
74+
createDBFolder();
75+
console.log('\x1b[1m\x1b[36m Starting Download ...\x1b[0m');
76+
getFile(DOWNLOAD_URL);
77+
}
78+
79+
run();

cms/tools/post-install.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function run() {
2+
console.log(
3+
'\x1b[34m please run \x1b[4m \x1b[32myarn boot\x1b[0m\x1b[34m if you have not already run it once'
4+
);
5+
console.log(
6+
'\x1b[0m\x1b[36m it will reload the database and reset the environmental variables \x1b[0m'
7+
);
8+
}
9+
10+
run();

0 commit comments

Comments
 (0)