-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Clean up extension package.json (#1101)
- Loading branch information
Showing
11 changed files
with
367 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,5 +160,7 @@ | |
"path": "test-runner" | ||
} | ||
], | ||
"settings": {} | ||
"settings": { | ||
"editor.formatOnSave": true | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Writable<T> = { -readonly [P in keyof T]: T[P] }; |
Oops, something went wrong.