forked from geosolutions-it/MapStore2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectLib.js
94 lines (86 loc) · 3.77 KB
/
projectLib.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const assign = require('object-assign');
const denodeify = require('denodeify');
const fs = require('fs');
const mkdirp = denodeify(require('mkdirp'));
const writeFile = denodeify(fs.writeFile);
const readdir = denodeify(fs.readdir);
const readFile = denodeify(fs.readFile);
const stat = denodeify(fs.stat);
const ncp = denodeify(require('ncp').ncp);
const createPackageJSON = (options, outFolder) => {
process.stdout.write('Creating package.json...\n');
const packageJSON = assign({}, require('./package.json'), options);
return writeFile(outFolder + '/package.json', JSON.stringify(packageJSON, null, 4));
};
const initGit = (outFolder) => {
process.stdout.write('Creating git repo...\n');
const git = require('simple-git')(outFolder);
return new Promise((resolve, reject) => {
git.init(() => {
git.submoduleAdd('https://github.com/geosolutions-it/MapStore2.git', 'MapStore2', (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
};
const copyTemplates = (basePath, outFolder, options, level = 0, path = '') => {
return readdir(basePath + path)
.then(files => {
return Promise.all(
files.map((file) => {
return stat(basePath + path + '/' + file)
.then((stats) => {
if (stats.isFile()) {
const filePath = basePath + path + '/' + file;
return readFile(filePath, 'UTF-8')
.then((content) => {
return mkdirp(outFolder + path)
.then(() => {
process.stdout.write('Copying ' + filePath + '\n');
const outContent = content
.replace(/__PROJECTNAME__/g, options.name)
.replace(/__PROJECTDESCRIPTION__/g, options.description)
.replace(/__PROJECTVERSION__/g, options.version)
.replace(/__REPOURL__/g, options.repository);
return writeFile(outFolder + path + '/' + file, outContent, 'UTF-8');
});
});
} else if (stats.isDirectory()) {
return copyTemplates(basePath, outFolder, options, level + 1, path + '/' + file);
}
return new Promise(resolve => { resolve(); });
});
})
);
});
};
const copyStaticFiles = (baseFolder, outFolder, options, baseFiles) => {
return new Promise((resolve, reject) => {
process.stdout.write('Copying static files...\n');
let copied = 0;
const toBeCopied = baseFiles.length;
baseFiles.map(function(fileName) {
const toWrite = fs.createWriteStream(outFolder + '/' + fileName);
fs.createReadStream(fileName).pipe(toWrite);
process.stdout.write('Copied ' + fileName + '\n');
return toWrite;
}).forEach(function(stream) {
stream.on('finish', function() {
copied++;
if (copied === toBeCopied) {
ncp(baseFolder, outFolder).then(resolve).catch(reject);
}
});
});
});
};
module.exports = {
initGit,
createPackageJSON,
copyTemplates,
copyStaticFiles
};