-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: skip reinstalling already installed plugin (#7)
- Loading branch information
Showing
7 changed files
with
357 additions
and
58 deletions.
There are no files selected for viewing
This file was deleted.
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,137 @@ | ||
import { spawn } from 'node:child_process' | ||
import fs from 'node:fs' | ||
|
||
import { PLUGINS_PACKAGE_JSON_PATH, PLUGIN_STORE_PATH } from './paths.js' | ||
import { splitPluginNameAndVersion } from './utils.js' | ||
|
||
const npmExecutable = process.platform.startsWith('win') ? 'npm.cmd' : 'npm' | ||
|
||
function listStoreDependencies(): [string, string][] { | ||
try { | ||
const packageJson = JSON.parse( | ||
fs.readFileSync(PLUGINS_PACKAGE_JSON_PATH, 'utf-8'), | ||
) as unknown | ||
|
||
if ( | ||
typeof packageJson === 'object' && | ||
packageJson && | ||
'dependencies' in packageJson && | ||
typeof packageJson.dependencies === 'object' && | ||
packageJson.dependencies | ||
) { | ||
return Object.entries(packageJson.dependencies) | ||
} | ||
|
||
return [] | ||
} catch { | ||
return [] | ||
} | ||
} | ||
|
||
function filterPluginsToInstall(pluginNames: string[]) { | ||
const storeDependencies = listStoreDependencies() | ||
const pluginsToInstall: string[] = [] | ||
const installedPlugins: string[] = [] | ||
|
||
pluginNames.forEach(rawPluginName => { | ||
let { name: pluginName, version: pluginVersion } = | ||
splitPluginNameAndVersion(rawPluginName) | ||
if ( | ||
pluginVersion && | ||
(pluginVersion.startsWith('^') || pluginVersion.startsWith('~')) | ||
) { | ||
pluginVersion = pluginVersion.slice(1) | ||
} | ||
|
||
const isPluginInstalled = storeDependencies.some( | ||
([dependencyName, dependencyVersion]) => { | ||
if (dependencyName !== pluginName) { | ||
return false | ||
} | ||
|
||
if (!pluginVersion) { | ||
return true | ||
} | ||
|
||
if ( | ||
dependencyVersion.startsWith('^') || | ||
dependencyVersion.startsWith('~') | ||
) { | ||
dependencyVersion = dependencyVersion.slice(1) | ||
} | ||
|
||
return dependencyVersion.startsWith(pluginVersion) | ||
}, | ||
) | ||
|
||
if (isPluginInstalled) { | ||
installedPlugins.push(pluginName) | ||
} else { | ||
pluginsToInstall.push(rawPluginName) | ||
} | ||
}) | ||
|
||
return { | ||
pluginsToInstall, | ||
installedPlugins, | ||
} | ||
} | ||
|
||
export async function installPlugins(pluginNames: string[]) { | ||
if (!pluginNames.length) { | ||
return | ||
} | ||
|
||
if (!fs.existsSync(PLUGIN_STORE_PATH)) { | ||
fs.mkdirSync(PLUGIN_STORE_PATH) | ||
} | ||
|
||
if (!fs.existsSync(PLUGINS_PACKAGE_JSON_PATH)) { | ||
fs.writeFileSync(PLUGINS_PACKAGE_JSON_PATH, '{}', { encoding: 'utf-8' }) | ||
} | ||
|
||
const { pluginsToInstall, installedPlugins } = | ||
filterPluginsToInstall(pluginNames) | ||
|
||
if (installedPlugins.length) { | ||
console.log('\n----- Already installed ----\n') | ||
installedPlugins.forEach(pluginName => console.log(` - ${pluginName}`)) | ||
} | ||
|
||
if (pluginsToInstall.length) { | ||
console.log('\n---- Installing plugins ----\n') | ||
pluginsToInstall.forEach(pluginName => console.log(` - ${pluginName}`)) | ||
|
||
const child = spawn( | ||
npmExecutable, | ||
[ | ||
'install', | ||
'--ignore-scripts', | ||
'--no-package-lock', | ||
'--no-lockfile', | ||
'--omit=dev', | ||
'--install-strategy=shallow', | ||
'--no-bin-links', | ||
'--no-global', | ||
...pluginsToInstall, | ||
], | ||
{ | ||
cwd: PLUGIN_STORE_PATH, | ||
env: Object.assign({}, process.env, { NODE_ENV: 'production' }), | ||
stdio: 'inherit', | ||
}, | ||
) | ||
|
||
return await new Promise<void>((resolve, reject) => { | ||
child.on('error', err => { | ||
reject(err) | ||
}) | ||
child.on('exit', code => { | ||
if (code === 0) { | ||
return resolve() | ||
} | ||
reject() | ||
}) | ||
}) | ||
} | ||
} |
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
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,19 @@ | ||
export function splitPluginNameAndVersion(packageName: string): { | ||
name: string | ||
version: string | undefined | ||
} { | ||
let name = '' | ||
|
||
if (packageName.startsWith('@')) { | ||
name = '@' | ||
packageName = packageName.slice(1) | ||
} | ||
|
||
name += packageName.split('@')[0] | ||
const version = packageName.split('@')[1] | ||
|
||
return { | ||
name, | ||
version, | ||
} | ||
} |
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
Oops, something went wrong.