1 | const fs = require('fs');
|
2 | const child_process = require('child_process');
|
3 |
|
4 | function createFile(id) {
|
5 | const data = { id: id.trim() };
|
6 | fs.writeFileSync(`${process.cwd()}/dist/id.json`, JSON.stringify(data));
|
7 | console.log(`Created Aspecto identifier ${id}`);
|
8 | }
|
9 |
|
10 | function create() {
|
11 | try {
|
12 | const rootPath = process.cwd() + '/';
|
13 | const rev = fs.readFileSync(`${rootPath}.git/HEAD`).toString();
|
14 | if (rev.indexOf(':') === -1) {
|
15 | createFile(rev);
|
16 | } else {
|
17 | const hash = readFileSync(`${rootPath}.git/${rev.substring(5).replace('\n', '')}`)
|
18 | .toString()
|
19 | .replace('\n', '');
|
20 | createFile(hash);
|
21 | }
|
22 | return;
|
23 | } catch (err) {}
|
24 |
|
25 | try {
|
26 | const gitData = child_process.execSync('git rev-parse HEAD', { stdio: 'ignore' }).toString();
|
27 | return createFile(gitData);
|
28 | } catch (err) {}
|
29 |
|
30 | try {
|
31 | const gitData = child_process.execSync('git rev-parse HEAD').toString();
|
32 | return createFile(gitData);
|
33 | } catch (err) {}
|
34 |
|
35 | try {
|
36 | createFile(`webapp-${new Date().toISOString()}`);
|
37 | } catch (err) {
|
38 | console.log('Failed generating aspecto identifier');
|
39 | }
|
40 | }
|
41 |
|
42 | create();
|