-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpublish.js
69 lines (60 loc) · 1.87 KB
/
publish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const fs = require('fs');
const path = require('path');
const Log = require('../Log');
const ipfsAPI = require('ipfs-api');
const JSONFile = require('jsonfile');
const openBrowser = require('opn');
const BuildPath = 'build';
const kaizenfile = "kaizen.json";
function fsExistsSync() {
try {
fs.accessSync(BuildPath, fs.constants.R_OK | fs.constants.W_OK);
return true;
} catch (err) {
return false;
}
}
function loopFilesInFolder(path, files) {
const readdirSyncs = fs.readdirSync(path);
readdirSyncs.forEach(item => {
if (item.includes('.DS_Store')) return;
switch (fs.statSync(`${path}/${item}`).isDirectory()) {
case true:
files = loopFilesInFolder(`${path}/${item}`, files);
break;
case false:
files.push(`${path}/${item}`);
break
}
});
return files;
}
function getIPFSContentObject(filePath, targetPath) {
return ({
path: `public${filePath.replace(targetPath, '')}`,
content: fs.readFileSync(filePath)
});
}
exports.description = 'publish you app to the IPFS';
exports.yargs = function (yargs) {
if (!fsExistsSync()) {
Log.ErrorLog("Build This folder does not exist");
}
const targetPath = `${path.resolve('./', BuildPath)}`;
try {
(async function(yargs, targetPath) {
const kaizenConfig = await JSONFile.readFile(kaizenfile)
const ipfs = ipfsAPI(kaizenConfig.provider);
console.log('=== uploading to the IPFS ===')
const files = loopFilesInFolder(targetPath, []).map(item => getIPFSContentObject(item, targetPath));
const hashes = await ipfs.files.add(files, { recursive: false });
const { hash, } = hashes[hashes.length - 1];
const iphsUrl = `https://ipfs.infura.io/ipfs/${hash}`;
openBrowser(iphsUrl);
Log.SuccessLog(`ipfs url => ${iphsUrl}`)
process.exit();
})(yargs, targetPath);
} catch (err) {
Log.ErrorLog(err)
}
}