Skip to content

Commit

Permalink
(feat) Improve app prompting logic and behaviour (openmrs#1006)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher authored May 20, 2024
1 parent a2b138a commit eeb1c8c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/tooling/openmrs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"postcss": "^8.4.6",
"postcss-loader": "^6.2.1",
"rimraf": "^3.0.2",
"semver": "^7.3.4",
"swc-loader": "^0.2.3",
"tar": "^6.0.5",
"typescript": "^4.6.4",
Expand All @@ -69,6 +70,7 @@
"@types/pacote": "^11.1.5",
"@types/react": "^18.0.9",
"@types/rimraf": "^2.0.2",
"@types/semver": "^7",
"@types/tar": "^4.0.3"
}
}
40 changes: 26 additions & 14 deletions packages/tooling/openmrs/src/commands/assemble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import rimraf from 'rimraf';
import axios from 'axios';
import npmRegistryFetch from 'npm-registry-fetch';
import pacote from 'pacote';
import semver from 'semver';
import { contentHash, logInfo, logWarn, untar } from '../utils';
import { getNpmRegistryConfiguration } from '../utils/npmConfig';

Expand All @@ -30,6 +31,7 @@ interface NpmSearchResult {
version: string;
};
}>;
total: number;
}

interface AssembleConfig {
Expand Down Expand Up @@ -103,16 +105,24 @@ async function readConfig(
logInfo(`Loading available frontend modules ...`);

const packages = await npmRegistryFetch
.json('/-/v1/search?text=keywords:openmrs&size=500', fetchOptions)
.then((res) => res as unknown as NpmSearchResult)
// see https://github.com/npm/registry/blob/main/docs/REGISTRY-API.md#get-v1search for what these
// options mean; in essence, we search for anything with the keyword openmrs that has at least one
// stable version; quality is down-scored because that metric favours smaller apps over core
// community assets. Maintenance is boosted to de-score relatively unmaintained apps, as the framework
// still has a fair bit of churn
.json(
`/-/v1/search?text=app%20keywords:openmrs&not:unstable&quality=0.001&maintenance=3.0&size=250`,
fetchOptions,
)
.then((res) =>
res.objects
(res as unknown as NpmSearchResult).objects
.map((m) => ({
name: m.package.name,
version: m.package.version,
}))
.filter((m) => m.name.endsWith('-app')),
);

const questions: Array<Question> = [];

for (const pckg of packages) {
Expand All @@ -125,14 +135,22 @@ async function readConfig(
},
{
name: pckg.name,
askAnswered: true,
message: `Version for "${pckg.name}"?`,
default: pckg.version,
type: 'string',
when(ans) {
return ans[pckg.name];
when: (ans) => ans[pckg.name],
validate: (input) => {
if (typeof input !== 'string') {
return `Expected a valid SemVer string, got ${typeof input}.`;
}

if (!semver.valid(input)) {
return `${input} does not appear to be a valid semver or version range. See https://semver.npmjs.com/#syntax-examples.`;
}

return true;
},
} as Question,
},
);
}

Expand All @@ -157,16 +175,11 @@ async function readConfig(
}

async function downloadPackage(
cacheDir: string,
esmName: string,
esmVersion: string,
baseDir: string,
fetchOptions: npmRegistryFetch.Options,
): Promise<Buffer> {
if (!existsSync(cacheDir)) {
await mkdir(cacheDir, { recursive: true });
}

if (esmVersion.startsWith('file:')) {
const source = resolve(baseDir, esmVersion.substring(5));
return readFile(source);
Expand Down Expand Up @@ -233,7 +246,6 @@ export async function runAssemble(args: AssembleArgs) {
logInfo(`Assembling dependencies and building import map and routes registry...`);

const { frontendModules = {}, publicUrl = '.' } = config;
const cacheDir = resolve(process.cwd(), '.cache');

if (args.fresh && existsSync(args.target)) {
await new Promise((resolve) => rimraf(args.target, resolve));
Expand All @@ -244,7 +256,7 @@ export async function runAssemble(args: AssembleArgs) {
await Promise.all(
Object.keys(frontendModules).map(async (esmName) => {
const esmVersion = frontendModules[esmName];
const tgzBuffer = await downloadPackage(cacheDir, esmName, esmVersion, process.cwd(), npmConf);
const tgzBuffer = await downloadPackage(esmName, esmVersion, process.cwd(), npmConf);

const baseDirName = `${esmName}`.replace(/^@/, '').replace(/\//, '-');
const [fileName, version] = await extractFiles(tgzBuffer, resolve(args.target, baseDirName));
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5192,6 +5192,13 @@ __metadata:
languageName: node
linkType: hard

"@types/semver@npm:^7":
version: 7.5.8
resolution: "@types/semver@npm:7.5.8"
checksum: 10/3496808818ddb36deabfe4974fd343a78101fa242c4690044ccdc3b95dcf8785b494f5d628f2f47f38a702f8db9c53c67f47d7818f2be1b79f2efb09692e1178
languageName: node
linkType: hard

"@types/semver@npm:^7.3.4, @types/semver@npm:^7.5.0":
version: 7.5.6
resolution: "@types/semver@npm:7.5.6"
Expand Down Expand Up @@ -13573,6 +13580,7 @@ __metadata:
"@types/pacote": "npm:^11.1.5"
"@types/react": "npm:^18.0.9"
"@types/rimraf": "npm:^2.0.2"
"@types/semver": "npm:^7"
"@types/tar": "npm:^4.0.3"
autoprefixer: "npm:^10.4.2"
axios: "npm:^0.21.1"
Expand All @@ -13591,6 +13599,7 @@ __metadata:
postcss: "npm:^8.4.6"
postcss-loader: "npm:^6.2.1"
rimraf: "npm:^3.0.2"
semver: "npm:^7.3.4"
swc-loader: "npm:^0.2.3"
tar: "npm:^6.0.5"
typescript: "npm:^4.6.4"
Expand Down

0 comments on commit eeb1c8c

Please sign in to comment.