2024-01-15 12:24:52 +03:00
|
|
|
import { execSync } from 'node:child_process';
|
2024-03-21 13:09:25 +03:00
|
|
|
import { generateKeyPairSync } from 'node:crypto';
|
2024-01-15 12:24:52 +03:00
|
|
|
import fs from 'node:fs';
|
|
|
|
import path from 'node:path';
|
|
|
|
|
|
|
|
const SELF_HOST_CONFIG_DIR = '/root/.affine/config';
|
|
|
|
|
2024-12-06 04:37:26 +03:00
|
|
|
function generateConfigFile() {
|
|
|
|
const content = fs.readFileSync('./dist/config/affine.js', 'utf-8');
|
2024-02-02 11:32:06 +03:00
|
|
|
return content.replace(
|
|
|
|
/(^\/\/#.*$)|(^\/\/\s+TODO.*$)|("use\sstrict";?)|(^.*eslint-disable.*$)/gm,
|
|
|
|
''
|
|
|
|
);
|
2024-01-15 12:24:52 +03:00
|
|
|
}
|
|
|
|
|
2024-12-06 04:37:26 +03:00
|
|
|
function generatePrivateKey() {
|
|
|
|
const key = generateKeyPairSync('ec', {
|
|
|
|
namedCurve: 'prime256v1',
|
|
|
|
}).privateKey.export({
|
|
|
|
type: 'sec1',
|
|
|
|
format: 'pem',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (key instanceof Buffer) {
|
|
|
|
return key.toString('utf-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Array<{ to: string; generator: () => string }>}
|
|
|
|
*/
|
|
|
|
const configFiles = [
|
|
|
|
{ to: 'affine.js', generator: generateConfigFile },
|
|
|
|
{ to: 'private.key', generator: generatePrivateKey },
|
|
|
|
];
|
|
|
|
|
2024-01-15 12:24:52 +03:00
|
|
|
function prepare() {
|
|
|
|
fs.mkdirSync(SELF_HOST_CONFIG_DIR, { recursive: true });
|
|
|
|
|
2024-12-06 04:37:26 +03:00
|
|
|
for (const { to, generator } of configFiles) {
|
|
|
|
const targetFilePath = path.join(SELF_HOST_CONFIG_DIR, to);
|
2024-01-15 12:24:52 +03:00
|
|
|
if (!fs.existsSync(targetFilePath)) {
|
|
|
|
console.log(`creating config file [${targetFilePath}].`);
|
2024-12-06 04:37:26 +03:00
|
|
|
fs.writeFileSync(targetFilePath, generator(), 'utf-8');
|
2024-03-21 13:09:25 +03:00
|
|
|
}
|
2024-01-15 12:24:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function runPredeployScript() {
|
|
|
|
console.log('running predeploy script.');
|
|
|
|
execSync('yarn predeploy', {
|
2024-02-27 10:22:21 +03:00
|
|
|
encoding: 'utf-8',
|
|
|
|
env: process.env,
|
|
|
|
stdio: 'inherit',
|
2024-01-15 12:24:52 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare();
|
|
|
|
runPredeployScript();
|