-
Notifications
You must be signed in to change notification settings - Fork 4
/
buildPython.js
132 lines (105 loc) · 3.32 KB
/
buildPython.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fs = require('fs');
const { glob } = require('glob');
const path = require('path');
const { execSync } = require('child_process');
const { exit } = require('process');
const os = require('os');
const SKIP = process.argv[2] === 'skip';
const destFolder = 'public/libs/converter/dist/';
const srcFolder = 'src/libs/converter/';
const saveFileName = (destFolder, fileName) => {
const data = JSON.stringify({ fileName: fileName });
if (!fs.existsSync(destFolder)) fs.mkdirSync(destFolder, { recursive: true });
fs.writeFileSync(destFolder + 'yaptide_converter.json', data);
console.log('yaptide_converter.json saved');
};
(async () => {
const PYTHON = (() => {
const pythonCmdArr = ['python3', 'python'];
const index = pythonCmdArr.findIndex(pythonCmd => {
console.log(`Checking for ${pythonCmd}`);
try {
execSync(`${pythonCmd} --version`);
return true;
} catch (e) {
return false;
}
});
if (index === -1) {
console.error('Python not found');
return exit(1);
}
return pythonCmdArr[index];
})();
console.log(`\nUsing: ${PYTHON}`);
const installedPath = await glob(destFolder + 'yaptide_converter-*-py3-none-any.whl').then(
files => files[0]
);
const installedFileName = path.basename(installedPath ?? '');
// skip installation if file exists
if (installedPath && SKIP === true) {
//file exists
console.log(`${installedFileName} is already installed`);
saveFileName(destFolder, installedFileName);
} else {
const measureTime = (label, callback) => {
console.log('Start: ' + label);
console.time(label);
try {
callback();
} catch (error) {
console.error(error.stdout.toString());
exit(1);
}
console.timeEnd(label);
};
const executeCommand = command => {
console.log(`Executing: ${command}`);
const output = execSync(command, {
encoding: 'utf-8',
cwd: srcFolder
});
console.log('Output:', output);
};
const venvCommandPrefix =
os.platform() === 'win32'
? 'powershell -NoProfile -Command "& { .\\venv\\Scripts\\Activate.ps1 ; '
: '. venv/bin/activate &&';
const venvCommandSuffix = os.platform() === 'win32' ? ' }"' : '';
measureTime('Create venv environment', () => {
executeCommand(`${PYTHON} -m venv venv`);
});
measureTime('Installing build module for python', () => {
executeCommand(
`${venvCommandPrefix} pip install "poetry ~= 1.8.2" ${venvCommandSuffix}`
);
});
measureTime('Building yaptide_converter', () => {
executeCommand(
`${venvCommandPrefix} poetry build --format wheel --no-ansi ${venvCommandSuffix}`
);
});
console.log('Checking destination folder');
if (!fs.existsSync(destFolder)) {
console.log('Creating folder ' + destFolder);
fs.mkdirSync(destFolder, { recursive: true });
}
const buildFilePath = await glob(
srcFolder + 'dist/yaptide_converter-*-py3-none-any.whl'
).then(files => files[0]);
const buildFileName = path.basename(buildFilePath ?? '');
const destFullPath = destFolder + buildFileName;
console.log('Copying yaptide_converter');
fs.copyFile(buildFilePath, destFullPath, err => {
if (err) {
console.error(err.message);
exit(1);
}
console.log('yaptide_converter was copied to destination');
console.log(buildFilePath);
console.log('=>');
console.log(destFullPath);
});
saveFileName(destFolder, buildFileName);
}
})();