2021-01-18 16:48:42 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2021-01-05 17:25:15 +02:00
|
|
|
import * as fs from 'fs-extra';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as process from 'process';
|
2021-01-06 22:23:23 +02:00
|
|
|
import validatePluginId from '@joplin/lib/services/plugins/utils/validatePluginId';
|
2021-01-18 16:37:27 +02:00
|
|
|
import { execCommand2, resolveRelativePathWithinDir, gitPullTry, gitRepoCleanTry, gitRepoClean } from '@joplin/tools/tool-utils.js';
|
2021-01-21 02:12:59 +02:00
|
|
|
import checkIfPluginCanBeAdded from './lib/checkIfPluginCanBeAdded';
|
|
|
|
import updateReadme from './lib/updateReadme';
|
2021-02-01 00:23:39 +02:00
|
|
|
import { NpmPackage } from './lib/types';
|
2021-01-28 01:57:08 +02:00
|
|
|
import gitCompareUrl from './lib/gitCompareUrl';
|
2021-06-01 10:39:31 +02:00
|
|
|
import commandUpdateRelease from './commands/updateRelease';
|
2021-08-31 23:37:56 +02:00
|
|
|
import { isJoplinPluginPackage, readJsonFile } from './lib/utils';
|
|
|
|
import { applyManifestOverrides, getObsoleteManifests, readManifestOverrides } from './lib/overrideUtils';
|
2021-01-12 17:29:08 +02:00
|
|
|
|
2021-01-05 17:25:15 +02:00
|
|
|
function pluginInfoFromSearchResults(results: any[]): NpmPackage[] {
|
|
|
|
const output: NpmPackage[] = [];
|
|
|
|
|
|
|
|
for (const r of results) {
|
2021-01-12 17:29:08 +02:00
|
|
|
if (!isJoplinPluginPackage(r)) continue;
|
2021-01-05 17:25:15 +02:00
|
|
|
|
|
|
|
output.push({
|
|
|
|
name: r.name,
|
|
|
|
version: r.version,
|
|
|
|
date: new Date(r.date),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:33:10 +02:00
|
|
|
async function checkPluginRepository(dirPath: string, dryRun: boolean) {
|
2021-01-05 17:25:15 +02:00
|
|
|
if (!(await fs.pathExists(dirPath))) throw new Error(`No plugin repository at: ${dirPath}`);
|
|
|
|
if (!(await fs.pathExists(`${dirPath}/.git`))) throw new Error(`Directory is not a Git repository: ${dirPath}`);
|
2021-01-06 21:43:05 +02:00
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
const previousDir = chdir(dirPath);
|
2021-08-31 23:37:56 +02:00
|
|
|
if (!dryRun) {
|
|
|
|
await gitRepoCleanTry();
|
|
|
|
await gitPullTry();
|
2021-01-06 21:43:05 +02:00
|
|
|
}
|
2021-08-31 23:37:56 +02:00
|
|
|
chdir(previousDir);
|
2021-01-05 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 17:29:08 +02:00
|
|
|
async function extractPluginFilesFromPackage(existingManifests: any, workDir: string, packageName: string, destDir: string): Promise<any> {
|
2021-01-21 02:12:59 +02:00
|
|
|
const previousDir = chdir(workDir);
|
2021-01-05 17:25:15 +02:00
|
|
|
|
2021-01-18 16:37:27 +02:00
|
|
|
await execCommand2(`npm install ${packageName} --save --ignore-scripts`, { showOutput: false });
|
2021-01-05 17:25:15 +02:00
|
|
|
|
|
|
|
const pluginDir = resolveRelativePathWithinDir(workDir, 'node_modules', packageName, 'publish');
|
|
|
|
|
|
|
|
const files = await fs.readdir(pluginDir);
|
2021-01-18 16:37:27 +02:00
|
|
|
const manifestFilePath = path.resolve(pluginDir, files.find((f: any) => path.extname(f) === '.json'));
|
|
|
|
const pluginFilePath = path.resolve(pluginDir, files.find((f: any) => path.extname(f) === '.jpl'));
|
2021-01-05 17:25:15 +02:00
|
|
|
|
|
|
|
if (!(await fs.pathExists(manifestFilePath))) throw new Error(`Could not find manifest file at ${manifestFilePath}`);
|
|
|
|
if (!(await fs.pathExists(pluginFilePath))) throw new Error(`Could not find plugin file at ${pluginFilePath}`);
|
|
|
|
|
2021-01-06 22:23:23 +02:00
|
|
|
// At this point, we need to check the manifest ID as it's used in various
|
|
|
|
// places including as directory name and object key in manifests.json, so
|
|
|
|
// it needs to be correct. It's mostly for security reasons. The other
|
|
|
|
// manifest properties are checked when the plugin is loaded into the app.
|
2021-01-06 21:43:05 +02:00
|
|
|
const manifest = await readJsonFile(manifestFilePath);
|
2021-01-06 22:23:23 +02:00
|
|
|
validatePluginId(manifest.id);
|
|
|
|
|
2021-01-06 21:43:05 +02:00
|
|
|
manifest._npm_package_name = packageName;
|
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
checkIfPluginCanBeAdded(existingManifests, manifest);
|
2021-01-05 17:25:15 +02:00
|
|
|
|
2021-01-06 21:43:05 +02:00
|
|
|
const pluginDestDir = resolveRelativePathWithinDir(destDir, manifest.id);
|
2021-01-05 17:25:15 +02:00
|
|
|
await fs.mkdirp(pluginDestDir);
|
|
|
|
|
2021-01-06 21:43:05 +02:00
|
|
|
await fs.writeFile(path.resolve(pluginDestDir, 'manifest.json'), JSON.stringify(manifest, null, '\t'), 'utf8');
|
2021-01-05 17:25:15 +02:00
|
|
|
await fs.copy(pluginFilePath, path.resolve(pluginDestDir, 'plugin.jpl'));
|
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
chdir(previousDir);
|
2021-01-05 17:25:15 +02:00
|
|
|
|
|
|
|
return manifest;
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:37:27 +02:00
|
|
|
interface CommandBuildArgs {
|
|
|
|
pluginRepoDir: string;
|
2021-02-07 16:33:10 +02:00
|
|
|
dryRun: boolean;
|
2021-01-18 16:37:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum ProcessingActionType {
|
|
|
|
Add = 1,
|
|
|
|
Update = 2,
|
|
|
|
}
|
|
|
|
|
2021-01-28 01:57:08 +02:00
|
|
|
function commitMessage(actionType: ProcessingActionType, manifest: any, previousManifest: any, npmPackage: NpmPackage, error: any): string {
|
2021-01-18 16:37:27 +02:00
|
|
|
const output: string[] = [];
|
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
if (!error) {
|
|
|
|
if (actionType === ProcessingActionType.Add) {
|
|
|
|
output.push('New');
|
|
|
|
} else {
|
|
|
|
output.push('Update');
|
|
|
|
}
|
|
|
|
|
|
|
|
output.push(`${manifest.id}@${manifest.version}`);
|
2021-01-18 16:37:27 +02:00
|
|
|
} else {
|
2021-01-21 02:12:59 +02:00
|
|
|
output.push(`Error: ${npmPackage.name}@${npmPackage.version}`);
|
2021-01-18 16:37:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-28 01:57:08 +02:00
|
|
|
const compareUrl = gitCompareUrl(manifest, previousManifest);
|
|
|
|
|
|
|
|
return output.join(': ') + (compareUrl ? `\n\n${compareUrl}` : '');
|
2021-01-18 16:37:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
function pluginManifestsPath(repoDir: string): string {
|
|
|
|
return path.resolve(repoDir, 'manifests.json');
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readManifests(repoDir: string): Promise<any> {
|
|
|
|
return readJsonFile(pluginManifestsPath(repoDir), {});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function writeManifests(repoDir: string, manifests: any) {
|
|
|
|
await fs.writeFile(pluginManifestsPath(repoDir), JSON.stringify(manifests, null, '\t'), 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
function chdir(path: string): string {
|
|
|
|
const previous = process.cwd();
|
|
|
|
try {
|
|
|
|
process.chdir(path);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Could not chdir to path: ${path}`);
|
|
|
|
}
|
|
|
|
return previous;
|
|
|
|
}
|
|
|
|
|
2021-08-31 23:37:56 +02:00
|
|
|
async function processNpmPackage(npmPackage: NpmPackage, repoDir: string, dryRun: boolean) {
|
2021-01-05 17:25:15 +02:00
|
|
|
const tempDir = `${repoDir}/temp`;
|
|
|
|
|
|
|
|
await fs.mkdirp(tempDir);
|
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
const originalPluginManifests = await readManifests(repoDir);
|
2021-08-31 23:37:56 +02:00
|
|
|
const manifestOverrides = await readManifestOverrides(repoDir);
|
|
|
|
const obsoleteManifests = getObsoleteManifests(manifestOverrides);
|
2021-01-12 17:29:08 +02:00
|
|
|
const existingManifests = {
|
|
|
|
...originalPluginManifests,
|
|
|
|
...obsoleteManifests,
|
|
|
|
};
|
2021-01-06 21:43:05 +02:00
|
|
|
|
2021-01-05 17:25:15 +02:00
|
|
|
const packageTempDir = `${tempDir}/packages`;
|
|
|
|
|
|
|
|
await fs.mkdirp(packageTempDir);
|
2021-01-21 02:12:59 +02:00
|
|
|
chdir(packageTempDir);
|
2021-01-18 16:37:27 +02:00
|
|
|
await execCommand2('npm init --yes --loglevel silent', { quiet: true });
|
2021-01-05 17:25:15 +02:00
|
|
|
|
2021-01-18 16:37:27 +02:00
|
|
|
let actionType: ProcessingActionType = ProcessingActionType.Update;
|
2021-01-06 21:43:05 +02:00
|
|
|
let manifests: any = {};
|
2021-01-21 02:12:59 +02:00
|
|
|
let manifest: any = {};
|
|
|
|
let error: any = null;
|
2021-01-28 01:57:08 +02:00
|
|
|
let previousManifest: any = null;
|
2021-01-06 21:43:05 +02:00
|
|
|
|
2021-01-18 16:37:27 +02:00
|
|
|
try {
|
|
|
|
const destDir = `${repoDir}/plugins/`;
|
2021-01-21 02:12:59 +02:00
|
|
|
manifest = await extractPluginFilesFromPackage(existingManifests, packageTempDir, npmPackage.name, destDir);
|
2021-01-18 16:37:27 +02:00
|
|
|
|
|
|
|
if (!existingManifests[manifest.id]) {
|
|
|
|
actionType = ProcessingActionType.Add;
|
2021-01-06 21:43:05 +02:00
|
|
|
}
|
2021-01-18 16:37:27 +02:00
|
|
|
|
2021-01-28 01:57:08 +02:00
|
|
|
if (!obsoleteManifests[manifest.id]) {
|
2021-02-07 16:33:10 +02:00
|
|
|
previousManifest = { ...originalPluginManifests[manifest.id] };
|
2021-01-28 01:57:08 +02:00
|
|
|
manifests[manifest.id] = manifest;
|
|
|
|
}
|
2021-01-21 02:12:59 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
error = e;
|
2021-01-05 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
if (!error) {
|
|
|
|
// We preserve the original manifests so that if a plugin has been removed
|
|
|
|
// from npm, we still keep it. It's also a security feature - it means that
|
|
|
|
// if a plugin is removed from npm, it's not possible to highjack it by
|
|
|
|
// creating a new npm package with the same plugin ID.
|
|
|
|
manifests = {
|
|
|
|
...originalPluginManifests,
|
|
|
|
...manifests,
|
|
|
|
};
|
|
|
|
|
2021-08-31 23:37:56 +02:00
|
|
|
manifests = applyManifestOverrides(manifests, manifestOverrides);
|
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
await writeManifests(repoDir, manifests);
|
|
|
|
await updateReadme(`${repoDir}/README.md`, manifests);
|
|
|
|
}
|
2021-01-11 14:42:11 +02:00
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
chdir(repoDir);
|
2021-01-05 17:25:15 +02:00
|
|
|
await fs.remove(tempDir);
|
2021-01-18 16:37:27 +02:00
|
|
|
|
2021-08-31 23:37:56 +02:00
|
|
|
if (!dryRun) {
|
|
|
|
if (!(await gitRepoClean())) {
|
|
|
|
await execCommand2('git add -A', { showOutput: false });
|
|
|
|
await execCommand2(['git', 'commit', '-m', commitMessage(actionType, manifest, previousManifest, npmPackage, error)], { showOutput: false });
|
|
|
|
} else {
|
|
|
|
console.info('Nothing to commit');
|
|
|
|
}
|
2021-01-18 16:37:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function commandBuild(args: CommandBuildArgs) {
|
2021-02-07 16:33:10 +02:00
|
|
|
const dryRun = !!args.dryRun;
|
2021-01-18 17:12:21 +02:00
|
|
|
console.info(new Date(), 'Building repository...');
|
2021-02-07 16:33:10 +02:00
|
|
|
if (dryRun) console.info('Dry run: on');
|
2021-01-18 17:12:21 +02:00
|
|
|
|
2021-01-18 16:37:27 +02:00
|
|
|
const repoDir = args.pluginRepoDir;
|
2021-02-07 16:33:10 +02:00
|
|
|
await checkPluginRepository(repoDir, dryRun);
|
2021-01-18 16:37:27 +02:00
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
// When starting, always update and commit README, in case something has
|
2021-08-31 23:37:56 +02:00
|
|
|
// been updated via a pull request. We do that separately so that the README
|
|
|
|
// update doesn't get mixed up with plugin updates, as in this example:
|
2021-01-21 02:12:59 +02:00
|
|
|
// https://github.com/joplin/plugins/commit/8a65bbbf64bf267674f854a172466ffd4f07c672
|
|
|
|
const manifests = await readManifests(repoDir);
|
|
|
|
await updateReadme(`${repoDir}/README.md`, manifests);
|
|
|
|
const previousDir = chdir(repoDir);
|
2021-08-31 23:37:56 +02:00
|
|
|
|
|
|
|
if (!dryRun) {
|
|
|
|
if (!(await gitRepoClean())) {
|
|
|
|
console.info('Updating README...');
|
|
|
|
await execCommand2('git add -A', { showOutput: true });
|
|
|
|
await execCommand2('git commit -m "Update README"', { showOutput: true });
|
|
|
|
}
|
2021-01-21 02:12:59 +02:00
|
|
|
}
|
2021-08-31 23:37:56 +02:00
|
|
|
|
2021-01-21 02:12:59 +02:00
|
|
|
chdir(previousDir);
|
|
|
|
|
2021-01-18 16:37:27 +02:00
|
|
|
const searchResults = (await execCommand2('npm search joplin-plugin --searchlimit 5000 --json', { showOutput: false })).trim();
|
|
|
|
const npmPackages = pluginInfoFromSearchResults(JSON.parse(searchResults));
|
|
|
|
|
|
|
|
for (const npmPackage of npmPackages) {
|
2021-08-31 23:37:56 +02:00
|
|
|
await processNpmPackage(npmPackage, repoDir, dryRun);
|
2021-01-18 16:37:27 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:39:31 +02:00
|
|
|
if (!dryRun) {
|
|
|
|
await commandUpdateRelease(args);
|
2021-08-31 23:37:56 +02:00
|
|
|
|
2021-06-01 10:39:31 +02:00
|
|
|
if (!(await gitRepoClean())) {
|
|
|
|
await execCommand2('git add -A', { showOutput: true });
|
|
|
|
await execCommand2('git commit -m "Update stats"', { showOutput: true });
|
|
|
|
}
|
|
|
|
|
2021-08-31 23:37:56 +02:00
|
|
|
await execCommand2('git push');
|
|
|
|
}
|
2021-01-18 16:37:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:12:21 +02:00
|
|
|
async function commandVersion() {
|
|
|
|
const p = await readJsonFile(path.resolve(__dirname, 'package.json'));
|
|
|
|
console.info(`Version ${p.version}`);
|
|
|
|
}
|
|
|
|
|
2021-01-18 16:37:27 +02:00
|
|
|
async function main() {
|
|
|
|
const scriptName: string = 'plugin-repo-cli';
|
|
|
|
|
|
|
|
const commands: Record<string, Function> = {
|
|
|
|
build: commandBuild,
|
2021-01-18 17:12:21 +02:00
|
|
|
version: commandVersion,
|
2021-06-01 10:39:31 +02:00
|
|
|
updateRelease: commandUpdateRelease,
|
2021-01-18 16:37:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let selectedCommand: string = '';
|
|
|
|
let selectedCommandArgs: string = '';
|
|
|
|
|
|
|
|
function setSelectedCommand(name: string, args: any) {
|
|
|
|
selectedCommand = name;
|
|
|
|
selectedCommandArgs = args;
|
|
|
|
}
|
|
|
|
|
|
|
|
require('yargs')
|
|
|
|
.scriptName(scriptName)
|
|
|
|
.usage('$0 <cmd> [args]')
|
2021-01-18 17:12:21 +02:00
|
|
|
|
2021-02-07 16:33:10 +02:00
|
|
|
.command('build <plugin-repo-dir> [dry-run]', 'Build the plugin repository', (yargs: any) => {
|
2021-01-18 16:37:27 +02:00
|
|
|
yargs.positional('plugin-repo-dir', {
|
|
|
|
type: 'string',
|
|
|
|
describe: 'Directory where the plugin repository is located',
|
|
|
|
});
|
|
|
|
}, (args: any) => setSelectedCommand('build', args))
|
2021-01-18 17:12:21 +02:00
|
|
|
|
|
|
|
.command('version', 'Gives version info', () => {}, (args: any) => setSelectedCommand('version', args))
|
|
|
|
|
2021-06-01 10:39:31 +02:00
|
|
|
.command('update-release <plugin-repo-dir>', 'Update GitHub release', () => {}, (args: any) => setSelectedCommand('updateRelease', args))
|
|
|
|
|
2021-01-18 16:37:27 +02:00
|
|
|
.help()
|
|
|
|
.argv;
|
|
|
|
|
|
|
|
if (!selectedCommand) {
|
|
|
|
console.error(`Please provide a command name or type \`${scriptName} --help\` for help`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!commands[selectedCommand]) {
|
|
|
|
console.error(`No such command: ${selectedCommand}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
await commands[selectedCommand](selectedCommandArgs);
|
2021-01-05 17:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((error) => {
|
|
|
|
console.error('Fatal error');
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
});
|