Skip to content

Commit

Permalink
Merge branch 'cluster' of github.com:koush/scrypted into cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Nov 29, 2024
2 parents 640d664 + 702456a commit 053b431
Show file tree
Hide file tree
Showing 11 changed files with 1,817 additions and 3,589 deletions.
2 changes: 1 addition & 1 deletion plugins/vscode-typescript
Submodule vscode-typescript updated 2 files
+2,016 −2,192 package-lock.json
+2 −2 package.json
2 changes: 1 addition & 1 deletion sdk/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ exports.deploy = function (debugHost, noRebind) {

return new Promise((resolve, reject) => {
var out;
if (process.env.NODE_ENV == 'production')
if (process.env.NODE_ENV === 'production')
out = path.resolve(process.cwd(), 'dist');
else
out = path.resolve(process.cwd(), 'out');
Expand Down
295 changes: 157 additions & 138 deletions sdk/bin/scrypted-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ const os = require('os');
const rimraf = require('rimraf');
const webpack = require('webpack');
const tmp = require('tmp');
const child_process = require('child_process');
const { once } = require('events');

if (fs.existsSync(path.resolve(cwd, 'src/main.py'))) {
let out;
if (process.env.NODE_ENV == 'production')
out = path.resolve(cwd, 'dist');
else
out = path.resolve(cwd, 'out');
let out;
if (process.env.NODE_ENV === 'production')
out = path.resolve(cwd, 'dist');
else
out = path.resolve(cwd, 'out');

if (fs.existsSync(path.resolve(cwd, 'src/main.py'))) {
const resolved = path.resolve(cwd, 'src');

const zip = new AdmZip();
Expand All @@ -40,171 +42,188 @@ if (fs.existsSync(path.resolve(cwd, 'src/main.py'))) {
if (fs.existsSync(zipfs))
zip.addLocalFolder(zipfs, 'fs');
zip.writeZip(path.join(out, 'plugin.zip'));
return;
}
else {
const packageJson = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json').toString()));
const optionalDependencies = Object.keys(packageJson.optionalDependencies || {});

if (packageJson.scrypted.babel) {
process.env.SCRYPTED_WEBPACK_BABEL = 'true';
}
const packageJson = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json').toString()));

const optionalDependencies = Object.keys(packageJson.optionalDependencies || {});

if (packageJson.scrypted.babel) {
process.env.SCRYPTED_WEBPACK_BABEL = 'true';
}

const defaultMainNodeJs = 'main.nodejs.js';
const entries = [];
if (packageJson.exports) {
for (const [key, value] of Object.entries(packageJson.exports)) {
const defaultMainNodeJs = 'main.nodejs.js';
const entries = [];
if (packageJson.exports) {
for (const [key, value] of Object.entries(packageJson.exports)) {
entries.push({
filename: key,
output: value,
});
}
}
else {
for (const search of ['src/main.js', 'src/main.ts']) {
const resolved = path.resolve(cwd, search);
if (fs.existsSync(resolved)) {
entries.push({
filename: key,
output: value,
filename: search,
output: defaultMainNodeJs,
});
break;
}
}
else {
for (const search of ['src/main.js', 'src/main.ts']) {
const resolved = path.resolve(cwd, search);
if (fs.existsSync(resolved)) {
entries.push({
filename: search,
output: defaultMainNodeJs,
});
break;
}
}
}
}

const nodeWebpackConfig = 'webpack.nodejs.config.js';
const nodeWebpackConfig = 'webpack.nodejs.config.js';

let out;
if (process.env.NODE_ENV == 'production')
out = path.resolve(cwd, 'dist');
else
out = path.resolve(cwd, 'out');
if (!entries?.length) {
console.warn('unable to locate src/main.js or src/main.ts');
console.warn('if a custom webpack config is used, will fall back to an entry configured there');
entries.push(undefined);
}

const zip = new AdmZip();

if (!entries?.length) {
console.warn('unable to locate src/main.js or src/main.ts');
console.warn('if a custom webpack config is used, will fall back to an entry configured there');
entries.push(undefined);
const readme = path.join(cwd, 'README.md');
if (fs.existsSync(readme)) {
let readmeText = fs.readFileSync(readme).toString();;
const changelog = path.join(cwd, 'CHANGELOG.md');
if (fs.existsSync(changelog)) {
readmeText += '\n\n\n<br/><br/>' + fs.readFileSync(changelog).toString();
}
zip.addFile('README.md', Buffer.from(readmeText));
}

let webpackCmd = path.resolve(cwd, 'node_modules/.bin/webpack-cli');
if (!fs.existsSync(webpackCmd)) {
webpackCmd = path.resolve(cwd, 'node_modules/@scrypted/sdk/node_modules/.bin/webpack-cli')
const NODE_PATH = path.resolve(__dirname, '..', 'node_modules');

// hack to override NODE_PATH dynamically.
// otherwise webpack plugins are not found.
process.env.NODE_PATH = NODE_PATH;
require('module').Module._initPaths();

async function rollup() {
if (out)
rimraf.sync(out);

let rollupCmd = path.resolve(cwd, 'node_modules/.bin/rollup');

if (!fs.existsSync(rollupCmd)) {
rollupCmd = path.resolve(cwd, 'node_modules/@scrypted/sdk/node_modules/.bin/rollup')
}
if (os.platform().startsWith('win')) {
webpackCmd += '.cmd';
rollupCmd += '.cmd';
}
const zip = new AdmZip();

const readme = path.join(cwd, 'README.md');
if (fs.existsSync(readme)) {
let readmeText = fs.readFileSync(readme).toString();;
const changelog = path.join(cwd, 'CHANGELOG.md');
if (fs.existsSync(changelog)) {
readmeText += '\n\n\n<br/><br/>' + fs.readFileSync(changelog).toString();
}
zip.addFile('README.md', Buffer.from(readmeText));
}
const cp = child_process.spawn(rollupCmd, [
'--config', path.resolve(__dirname, '../rollup.nodejs.config.mjs'),
], {
stdio: 'inherit',
});

const NODE_PATH = path.resolve(__dirname, '..', 'node_modules');
await once(cp, 'exit');
if (cp.exitCode)
throw new Error('rollup failed');

// hack to override NODE_PATH dynamically.
// otherwise webpack plugins are not found.
process.env.NODE_PATH = NODE_PATH;
require('module').Module._initPaths();
finishZip();
}

async function pack() {
if (out)
rimraf.sync(out);
async function pack() {
if (out)
rimraf.sync(out);

await new Promise((resolve, reject) => {
let webpackConfig;
const customWebpackConfig = path.resolve(cwd, nodeWebpackConfig);
const defaultWebpackConfig = path.resolve(__dirname, '..', nodeWebpackConfig);
if (fs.existsSync(customWebpackConfig)) {
webpackConfig = customWebpackConfig;
}
else {
webpackConfig = defaultWebpackConfig;
}
await new Promise((resolve, reject) => {
let webpackConfig;
const customWebpackConfig = path.resolve(cwd, nodeWebpackConfig);
const defaultWebpackConfig = path.resolve(__dirname, '..', nodeWebpackConfig);
if (fs.existsSync(customWebpackConfig)) {
webpackConfig = customWebpackConfig;
}
else {
webpackConfig = defaultWebpackConfig;
}

process.env.SCRYPTED_DEFAULT_WEBPACK_CONFIG = defaultWebpackConfig;
process.env.SCRYPTED_DEFAULT_WEBPACK_CONFIG = defaultWebpackConfig;

const webpackEntries = {};
const config = require(webpackConfig);
for (let entry of entries) {
entry ||= {
filename: config?.entry?.main,
output: defaultMainNodeJs,
};
const webpackEntries = {};
const config = require(webpackConfig);
for (let entry of entries) {
entry ||= {
filename: config?.entry?.main,
output: defaultMainNodeJs,
};

if (!entry?.filename) {
console.error("no main.ts or main.js was found, and webpack config does not supply an entry file.");
console.error(entry?.filename);
throw new Error();
}
if (!entry?.filename) {
console.error("no main.ts or main.js was found, and webpack config does not supply an entry file.");
console.error(entry?.filename);
throw new Error();
}

const main = path.resolve(cwd, entry.filename);
if (!fs.existsSync(main)) {
console.error("entry file specified in webpack config does not exist");
throw new Error();
}
const main = path.resolve(cwd, entry.filename);
if (!fs.existsSync(main)) {
console.error("entry file specified in webpack config does not exist");
throw new Error();
}


webpackEntries[entry?.output] = main;
}
webpackEntries[entry?.output] = main;
}


config.entry = webpackEntries;
config.output.filename = '[name]';
config.output.path = out;
for (const opt of optionalDependencies) {
const t = tmp.tmpNameSync({
postfix: '.js',
});
fs.writeFileSync(t, `
const e = __non_webpack_require__('${opt}');
module.exports = e;
`);
config.resolve.alias[opt] = t;
}
config.entry = webpackEntries;
config.output.filename = '[name]';
config.output.path = out;
for (const opt of optionalDependencies) {
const t = tmp.tmpNameSync({
postfix: '.js',
});
fs.writeFileSync(t, `
const e = __non_webpack_require__('${opt}');
module.exports = e;
`);
config.resolve.alias[opt] = t;
}

webpack(config, (err, stats) => {
if (err)
return reject(err);
webpack(config, (err, stats) => {
if (err)
return reject(err);

if (stats.hasErrors()) {
console.error(stats.toJson().errors);
return reject(new Error('webpack failed'));
}
if (stats.hasErrors()) {
console.error(stats.toJson().errors);
return reject(new Error('webpack failed'));
}

resolve();
})
});
resolve();
})
});

// create a zip that has a main.nodejs.js in the root, and an fs folder containing a read only virtual file system.
// todo: read write file system? seems like a potential sandbox and backup nightmare to do a real fs. scripts should
// use localStorage, etc?
const jsFiles = fs.readdirSync(out, {
withFileTypes: true
}).filter(ft => ft.isFile() && ft.name.endsWith('.js')).map(ft => ft.name);
for (const js of jsFiles) {
zip.addLocalFile(path.join(out, js));
const sourcemap = path.join(out, js + '.map');
if (fs.existsSync(sourcemap))
zip.addLocalFile(sourcemap);
console.log(js);
}
finishZip();
}

const zipfs = path.join(cwd, 'fs');
if (fs.existsSync(zipfs))
zip.addLocalFolder(zipfs, 'fs');
zip.writeZip(path.join(out, 'plugin.zip'));
function finishZip() {
// create a zip that has a main.nodejs.js in the root, and an fs folder containing a read only virtual file system.
// todo: read write file system? seems like a potential sandbox and backup nightmare to do a real fs. scripts should
// use localStorage, etc?
const jsFiles = fs.readdirSync(out, {
withFileTypes: true
}).filter(ft => ft.isFile() && ft.name.endsWith('.js')).map(ft => ft.name);
for (const js of jsFiles) {
zip.addLocalFile(path.join(out, js));
const sourcemap = path.join(out, js + '.map');
if (fs.existsSync(sourcemap))
zip.addLocalFile(sourcemap);
console.log(js);
}

pack()
.catch(e => process.nextTick(() => {
console.error(e);
throw new Error(e);
}));
const zipfs = path.join(cwd, 'fs');
if (fs.existsSync(zipfs))
zip.addLocalFolder(zipfs, 'fs');
zip.writeZip(path.join(out, 'plugin.zip'));
}

}
(packageJson.scrypted.rollup ? rollup : pack)()
.catch(e => process.nextTick(() => {
console.error(e);
throw new Error(e);
}));
Loading

0 comments on commit 053b431

Please sign in to comment.