2
0
mirror of https://github.com/frappe/books.git synced 2025-02-09 07:29:07 +00:00

incr: add script to build plugins

This commit is contained in:
18alantom 2023-07-19 14:26:44 +05:30
parent 33bdd6015d
commit 16eae085ff
3 changed files with 174 additions and 0 deletions

159
build/scripts/plugins.mjs Normal file
View File

@ -0,0 +1,159 @@
import yargs from 'yargs';
import esbuild from 'esbuild';
import { hideBin } from 'yargs/helpers';
import fs from 'fs-extra';
import path from 'path';
import { fileURLToPath } from 'url';
import { constants } from 'fs';
import { excludeVendorFromSourceMap } from './helpers.mjs';
import AdmZip from 'adm-zip';
const argv = yargs(hideBin(process.argv))
.option('list', {
describe: 'list plugins under development',
type: 'boolean',
})
.option('init', {
describe: 'initialize a new plugin in the plugin folder',
type: 'string',
})
.option('build', {
describe: 'build a plugin from the plugins folder',
type: 'string',
})
.help().argv;
const dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(dirname, '..', '..', 'plugins');
const buildDirPath = path.join(root, '..', 'dist_electron', 'plugins');
if (argv.list) {
listPlugins();
} else if (typeof argv.init === 'string') {
console.log('to be added');
} else if (typeof argv.build === 'string') {
await buildPlugin(argv.build);
}
async function buildPlugin(pluginName) {
if (pluginName === '') {
pluginName = fs.readdirSync(root)[0];
}
if (!pluginName) {
console.log('no plugins found');
return;
}
const pluginPath = path.join(root, pluginName);
const infoFile = fs.readFileSync(path.join(pluginPath, 'info.json'), 'utf-8');
const info = JSON.parse(infoFile);
/**
* Update info
*/
const packageJson = JSON.parse(
fs.readFileSync(path.join(root, '..', 'package.json'))
);
info.date = new Date().toISOString();
info.books_version = packageJson.version;
console.log(`building: ${info.name}`);
/**
* Create folder for plugin build.
* Plugin package will be stored in `pluginBuildPath/..`
*/
const pluginBuildPath = path.join(buildDirPath, pluginName, 'build');
fs.ensureDirSync(pluginBuildPath);
/**
* Write info file into the pluginBuildPath
*/
fs.writeFileSync(
path.join(pluginBuildPath, 'info.json'),
JSON.stringify(info)
);
/**
* Get entry points for esbuild to build from
*/
const entryPoints = getEntryPoints(pluginPath);
/**
* Build files from entry points.
*/
for (const key in entryPoints) {
const entryPath = entryPoints[key];
const result = await esbuild.build({
entryPoints: [entryPath],
sourcemap: 'inline',
sourcesContent: false,
bundle: true,
platform: 'node',
target: 'node16',
outfile: path.join(pluginBuildPath, `${key}.js`),
external: ['knex', 'electron', 'better-sqlite3', 'electron-store'],
plugins: [excludeVendorFromSourceMap],
write: true,
});
if (result.errors.length) {
console.error('app build failed due to main process source build');
result.errors.forEach((err) => console.error(err));
process.exit(1);
}
}
await createPluginPackage(pluginName, pluginBuildPath);
}
async function createPluginPackage(pluginName, pluginBuildPath) {
const zip = new AdmZip();
for (const file of fs.readdirSync(pluginBuildPath)) {
const filePath = path.join(pluginBuildPath, file);
zip.addLocalFile(filePath);
}
const packagePath = path.join(
pluginBuildPath,
'..',
`${pluginName}.books_plugin`
);
await zip.writeZipPromise(packagePath);
}
function getEntryPoints(pluginPath) {
const entryPoints = {};
for (const component of ['schemas', 'models']) {
const entryPointPath = path.join(pluginPath, component, 'index.ts');
try {
fs.accessSync(entryPointPath, constants.F_OK | constants.R_OK);
entryPoints[component] = entryPointPath;
} catch {
continue;
}
}
return entryPoints;
}
function listPlugins() {
for (const plugin of fs.readdirSync(root)) {
const infoPath = path.join(root, plugin, 'info.json');
let info;
try {
const infoFile = fs.readFileSync(infoPath, 'utf-8');
info = JSON.parse(infoFile);
} catch {
info = { name: plugin };
}
info.path = path.join(root, plugin);
console.log(plugin);
console.log(
JSON.stringify(info, null, 2).split('\n').slice(1, -1).join('\n'),
'\n'
);
}
}

View File

@ -8,6 +8,7 @@
},
"scripts": {
"dev": "node build/scripts/dev.mjs",
"plugins": "node build/scripts/plugins.mjs",
"build": "node build/scripts/build.mjs",
"release": "scripts/publish-mac-arm.sh",
"postinstall": "electron-rebuild",
@ -23,6 +24,7 @@
"@codemirror/autocomplete": "^6.4.2",
"@codemirror/lang-vue": "^0.1.1",
"@popperjs/core": "^2.10.2",
"adm-zip": "^0.5.10",
"better-sqlite3": "^7.5.3",
"codemirror": "^6.0.1",
"core-js": "^3.19.0",
@ -42,6 +44,7 @@
"@codemirror/state": "^6.0.0",
"@codemirror/view": "^6.0.0",
"@lezer/common": "^1.0.0",
"@types/adm-zip": "^0.5.0",
"@types/assert": "^1.5.6",
"@types/better-sqlite3": "^7.6.4",
"@types/electron-devtools-installer": "^2.2.0",

View File

@ -580,6 +580,13 @@
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==
"@types/adm-zip@^0.5.0":
version "0.5.0"
resolved "https://registry.yarnpkg.com/@types/adm-zip/-/adm-zip-0.5.0.tgz#94c90a837ce02e256c7c665a6a1eb295906333c1"
integrity sha512-FCJBJq9ODsQZUNURo5ILAQueuA8WJhRvuihS3ke2iI25mJlfV2LK8jG2Qj2z2AWg8U0FtWWqBHVRetceLskSaw==
dependencies:
"@types/node" "*"
"@types/assert@^1.5.6":
version "1.5.6"
resolved "https://registry.yarnpkg.com/@types/assert/-/assert-1.5.6.tgz#a8b5a94ce5fb8f4ba65fdc37fc9507609114189e"
@ -1060,6 +1067,11 @@ acorn@^8.8.0:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.9.0.tgz#78a16e3b2bcc198c10822786fa6679e245db5b59"
integrity sha512-jaVNAFBHNLXspO543WnNNPZFRtavh3skAkITqD0/2aeMkKZTN+254PyhwxFYrk3vQ1xfY+2wbesJMs/JC8/PwQ==
adm-zip@^0.5.10:
version "0.5.10"
resolved "https://registry.yarnpkg.com/adm-zip/-/adm-zip-0.5.10.tgz#4a51d5ab544b1f5ce51e1b9043139b639afff45b"
integrity sha512-x0HvcHqVJNTPk/Bw8JbLWlWoo6Wwnsug0fnYYro1HBrjxZ3G7/AZk7Ahv8JwDe1uIcz8eBqvu86FuF1POiG7vQ==
agent-base@6, agent-base@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"