Skip to content

Commit

Permalink
chore: Clean up extension package.json (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Jan 5, 2025
1 parent f73becb commit 3cf9fa2
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 100 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/manual-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ on:
inputs:
ref:
description: >-
The branch, tag or SHA to checkout. When checking out the repository
that triggered a workflow, this defaults to the reference or SHA for
that event. Otherwise, uses the default branch.
The branch, tag or SHA to checkout. When checking out the repository that triggered a workflow, this defaults
to the reference or SHA for that event. Otherwise, uses the default branch.
required: true
default: main
extension:
Expand Down Expand Up @@ -50,6 +49,7 @@ on:
- extensions/latin
- extensions/latvian
- extensions/lithuanian
- extensions/macedonian
- extensions/medical-terms
- extensions/norwegian-bokmal
- extensions/persian
Expand All @@ -65,6 +65,7 @@ on:
- extensions/spanish
- extensions/swedish
- extensions/swiss-german
- extensions/timestamp-hover
- extensions/turkish
- extensions/ukrainian
- extensions/vietnamese
Expand Down Expand Up @@ -116,4 +117,5 @@ jobs:
secrets:
VSCE_TOKEN: ${{ secrets.VSCE_TOKEN }}
OVSX_TOKEN: ${{ secrets.OVSX_TOKEN }}

# cspell:ignore vsix xargs OVSX
4 changes: 3 additions & 1 deletion dict-extensions.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,7 @@
"path": "test-runner"
}
],
"settings": {}
"settings": {
"editor.formatOnSave": true
}
}
18 changes: 0 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions scripts/fix-extensions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

/**
* Fix extensions of files.
*/

// @ts-check

import {
fixExtensionPackageJson,
getExtensionList,
readExtensionPackageJson,
writeExtensionPackageJson,
} from './lib/extensionHelper.mjs';

async function run() {
console.log('Fixing extensions...');
const extensions = await getExtensionList();

for (const ext of extensions) {
const pkg = await readExtensionPackageJson(ext);
fixExtensionPackageJson(ext, pkg);
await writeExtensionPackageJson(ext, pkg);
}
}

await run();
65 changes: 13 additions & 52 deletions scripts/gen-extension-list.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#!/usr/bin/env node

// ts-check
// @ts-check
import * as fs from 'node:fs/promises';
import { fileURLToPath } from 'url';
import * as path from 'path';
import { toMarkdown } from 'mdast-util-to-markdown';
import { root, paragraph, text, heading, list, listItem, link } from 'mdast-builder';

import { root, paragraph, text, heading, list, listItem, link } from './lib/mdastBuilder.mjs';

import { getExtensionInfo } from './lib/extensionHelper.mjs';

/**
* @typedef {import('./lib/extensionHelper.mjs').ExtensionInfo} ExtensionInfo
*/

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand All @@ -15,6 +22,9 @@ const targetExtensionFolderListMarkdown = path.join(__root, 'static/generated/ex
const targetMarketplaceLanguageExtensions = path.join(__root, 'static/generated/marketplace_language_extensions.md');
const targetMarketplaceExtensions = path.join(__root, 'static/generated/marketplace_extensions.md');

/**
* @type {import('mdast-util-to-markdown').Options}
*/
const markdownOptions = {
bullet: '-',
};
Expand Down Expand Up @@ -43,7 +53,7 @@ function pathToLink(url, name) {
function makeExtensionListItem(extensionInfo) {
return listItem(
paragraph([
link(extensionInfo.extensionPath + '#readme', undefined, text(extensionInfo.displayNameShort)),
link(extensionInfo.extensionPath + '#readme', text(extensionInfo.displayNameShort)),
text(` - ${extensionInfo.description}`),
]),
);
Expand All @@ -59,62 +69,13 @@ function makeMarketplaceExtensionListItem(extensionInfo) {
paragraph([
link(
'https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.' + extensionInfo.name,
undefined,
text(extensionInfo.displayNameShort),
),
text(' - ' + extensionInfo.description),
]),
);
}

/**
*
* @typedef {Object} ExtensionInfo
* @property {string} name
* @property {string} displayName
* @property {string} displayNameShort
* @property {string} version
* @property {string} description
* @property {string} extensionPath
* @property {string} dictionaryType
*/

/** @type {import('../static/dictionary-types.json')} */
const dictionaryTypes = JSON.parse(await fs.readFile(path.join(__root, 'static/dictionary-types.json')));

/**
*
* @param {string} extensionPath
* @returns {string}
*/
function lookUpDictionaryType(extensionPath) {
for (const [category, extensions] of Object.entries(dictionaryTypes.types)) {
if (extensions.includes(extensionPath)) return category;
}

return dictionaryTypes.default;
}

/**
*
* @param {string} extensionPath
* @returns {Promise<ExtensionInfo>}
*/
async function getExtensionInfo(extensionPath) {
const pkg = JSON.parse(await fs.readFile(path.join(__root, extensionPath, 'package.json'), 'utf8'));

/** @type {ExtensionInfo} */
const info = {
name: pkg.name,
description: pkg.description,
displayName: pkg.displayName,
displayNameShort: pkg.displayName.replace(/ -.*/, '').trim(),
version: pkg.version,
extensionPath,
dictionaryType: lookUpDictionaryType(extensionPath),
};
return info;
}

/**
*
Expand Down
165 changes: 165 additions & 0 deletions scripts/lib/extensionHelper.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// @ts-check
import fs from 'node:fs/promises';
import pathPosix from 'node:path/posix';

const rootUrl = new URL('../../', import.meta.url);
const extensionsUrl = new URL('extensions/', rootUrl);

const repositoryUrl = new URL('https://github.com/streetsidesoftware/vscode-cspell-dict-extensions');
const repositoryRawUrl = new URL(
'https://raw.githubusercontent.com/streetsidesoftware/vscode-cspell-dict-extensions/refs/heads/main/',
);

/** @type {Repository} */
const defaultRepository = {
type: 'git',
url: repositoryUrl.href,
};

/**
* @returns {Promise<string[]>}
*/
export async function getExtensionList() {
const extensionFolders = await fs.readdir(extensionsUrl, { withFileTypes: true });
return extensionFolders
.filter((f) => f.isDirectory())
.map((f) => new URL(f.name + '/', extensionsUrl))
.map((u) => urlRelativeToRoot(u));
}

/**
* return a relative path to the repository root.
* @param {URL} url
* @returns {string}
*/
export function urlRelativeToRoot(url) {
const addSlash = url.pathname.endsWith('/') ? '/' : '';
return pathPosix.relative(rootUrl.pathname, url.pathname) + addSlash;
}

/**
* @typedef {Object} ExtensionInfo
* @property {string} name
* @property {string} displayName
* @property {string} displayNameShort
* @property {string} version
* @property {string} description
* @property {string} extensionPath
* @property {string} dictionaryType
*/

/**
*
* @param {string} extensionPath
* @returns {Promise<ExtensionInfo>}
*/
export async function getExtensionInfo(extensionPath) {
const pkgUrl = new URL(pathPosix.join(extensionPath, 'package.json'), rootUrl);
const pkg = JSON.parse(await fs.readFile(pkgUrl, 'utf8'));

/** @type {ExtensionInfo} */
const info = {
name: pkg.name,
description: pkg.description,
displayName: pkg.displayName,
displayNameShort: pkg.displayName.replace(/ -.*/, '').trim(),
version: pkg.version,
extensionPath,
dictionaryType: lookUpDictionaryType(extensionPath),
};
return info;
}

/** @type {import('../../static/dictionary-types.json')} */
const dictionaryTypes = JSON.parse(await fs.readFile(new URL('static/dictionary-types.json', rootUrl), 'utf8'));

/**
*
* @param {string} extensionPath
* @returns {string}
*/
function lookUpDictionaryType(extensionPath) {
for (const [category, extensions] of Object.entries(dictionaryTypes.types)) {
if (extensions.includes(extensionPath)) return category;
}

return dictionaryTypes.default;
}

/**
* @template T
* @typedef {import('./index').Writable<T>} Writable
*/

/**
* @typedef {Writable<import('@vscode/vsce').IPackageOptions>} VSCEPackageOptions
*/

/**
* @typedef {Object} Repository
* @property {'git'} type
* @property {string} url
* @property {string} [directory]
*/

/**
* @typedef {Object} PackageJson
* @property {string} name
* @property {string} displayName
* @property {string} version
* @property {string} description
* @property {string} main
* @property {string} [browser]
* @property {Repository} [repository]
* @property {VSCEPackageOptions} [vsce]
* @property {boolean} private
* @property {boolean | undefined} [preview]
*/

/**
*
* @param {URL | string} pkgUrl
* @returns {Promise<PackageJson>}
*/
export async function readPackageJson(pkgUrl) {
return JSON.parse(await fs.readFile(pkgUrl, 'utf8'));
}

/**
*
* @param {string | URL} extensionPath
* @returns {Promise<PackageJson>}
*/
export async function readExtensionPackageJson(extensionPath) {
const extUrl = new URL(extensionPath, rootUrl);
const pkgUrl = new URL('package.json', extUrl);
return readPackageJson(pkgUrl);
}

/**
*
* @param {string | URL} extensionPath
* @param {PackageJson} pkg
*/
export async function writeExtensionPackageJson(extensionPath, pkg) {
const extUrl = new URL(extensionPath, rootUrl);
const pkgUrl = new URL('package.json', extUrl);
await fs.writeFile(pkgUrl, JSON.stringify(pkg, null, 2) + '\n');
}

/**
* @param {string | URL} extensionDir
* @param {PackageJson} pkg
* @returns {PackageJson}
*/
export function fixExtensionPackageJson(extensionDir, pkg) {
extensionDir = urlRelativeToRoot(new URL(extensionDir, rootUrl));
pkg.private = true;
pkg.preview = undefined;
pkg.repository = pkg.repository || { ...defaultRepository };
pkg.repository.directory = extensionDir.replace(/\/$/, '');
pkg.vsce = pkg.vsce || {};
pkg.vsce.baseContentUrl = new URL(extensionDir, repositoryRawUrl).href;
pkg.vsce.baseImagesUrl = new URL(extensionDir, repositoryRawUrl).href;
return pkg;
}
1 change: 1 addition & 0 deletions scripts/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Writable<T> = { -readonly [P in keyof T]: T[P] };
Loading

0 comments on commit 3cf9fa2

Please sign in to comment.