Skip to content

Commit

Permalink
cli: Add check:theia-extensions command (eclipse-theia#12596)
Browse files Browse the repository at this point in the history
This allows to check for duplicate versions of theia extensions only.
Added the option `only-theia-extensions` to `theia check:dependencies`.
Theia extensions are identified via their `theiaExtensions` field in the `package.json`.
The check is running recursively as long as it finds Theia extensions.
The advantage of this approach is that a shorter, but more important, list is returned.

Contributed on behalf of STMicroelectronics
  • Loading branch information
sgraband authored Jul 14, 2023
1 parent 84fe3d9 commit dab0aad
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## v1.40.0 -

- Show command shortcuts in toolbar item tooltips. #12660 (https://github.com/eclipse-theia/theia/pull/12660) - Contributed on behalf of STMicroelectronics
- [cli] added `check:theia-extensions` which checks the uniqueness of Theia extension versions [#12596](https://github.com/eclipse-theia/theia/pull/12596) - Contributed on behalf of STMicroelectronics

<a name="breaking_changes_1.40.0">[Breaking Changes:](#breaking_changes_1.40.0)</a>

Expand Down
28 changes: 23 additions & 5 deletions dev-packages/cli/src/check-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface CheckDependenciesOptions {
skipHoisted: boolean,
skipUniqueness: boolean,
skipSingleTheiaVersion: boolean,
onlyTheiaExtensions: boolean,
suppress: boolean
}

Expand All @@ -46,7 +47,9 @@ interface Package {
/** Whether the package is hoisted or not, i.e., whether it is contained in the root `node_modules`. */
hoisted: boolean,
/** Workspace location in which the package was found. */
dependent: string | undefined
dependent: string | undefined,
/** Whether the package is a Theia extension or not */
isTheiaExtension?: boolean,
}

/** Issue found with a specific package. */
Expand Down Expand Up @@ -122,9 +125,14 @@ function findDependencies(workspace: string, options: CheckDependenciesOptions):
`[^@]*/*/**/${PACKAGE_JSON}`, // package.json that isn't at the package root (and not in an @org)
`@*/*/*/**/${PACKAGE_JSON}`, // package.json that isn't at the package root (and in an @org)
...options.exclude] // user-specified exclude patterns
}).forEach(packageJson =>
matchingPackageJsons.push(toDependency(packageJson, nodeModulesDir, dependent))
)
}).forEach(packageJsonPath => {
const dependency = toDependency(packageJsonPath, nodeModulesDir, dependent);
if (!options.onlyTheiaExtensions || dependency.isTheiaExtension) {
matchingPackageJsons.push(dependency);
}
const childNodeModules: string = path.join(nodeModulesDir, packageJsonPath, '..');
matchingPackageJsons.push(...findDependencies(childNodeModules, options));
})
);
return matchingPackageJsons;
}
Expand All @@ -138,7 +146,8 @@ function toDependency(packageJsonPath: string, nodeModulesDir: string, dependent
version: version ?? 'unknown',
path: path.relative(process.cwd(), fullPackageJsonPath),
hoisted: nodeModulesDir === NODE_MODULES,
dependent: dependent
dependent: dependent,
isTheiaExtension: isTheiaExtension(fullPackageJsonPath)
};
}

Expand All @@ -158,6 +167,15 @@ function getPackageName(fullPackageJsonPath: string): string | undefined {
}
}

function isTheiaExtension(fullPackageJsonPath: string): boolean {
try {
const theiaExtension = require(fullPackageJsonPath).theiaExtensions;
return theiaExtension ? true : false;
} catch (error) {
return false;
}
}

function analyzeDependencies(packages: Package[], options: CheckDependenciesOptions): DependencyIssue[] {
const issues: DependencyIssue[] = [];
if (!options.skipHoisted) {
Expand Down
37 changes: 37 additions & 0 deletions dev-packages/cli/src/theia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ async function theiaCli(): Promise<void> {
skipHoisted: false,
skipUniqueness: true,
skipSingleTheiaVersion: true,
onlyTheiaExtensions: false,
suppress
});
}
Expand All @@ -226,6 +227,33 @@ async function theiaCli(): Promise<void> {
skipHoisted: true,
skipUniqueness: false,
skipSingleTheiaVersion: false,
onlyTheiaExtensions: false,
suppress
});
}
})
.command<{
suppress: boolean
}>({
command: 'check:theia-extensions',
describe: 'Check uniqueness of Theia extension versions or whether they are hoisted',
builder: {
'suppress': {
alias: 's',
describe: 'Suppress exiting with failure code',
boolean: true,
default: false
}
},
handler: ({ suppress }) => {
checkDependencies({
workspaces: undefined,
include: ['**'],
exclude: [],
skipHoisted: true,
skipUniqueness: false,
skipSingleTheiaVersion: true,
onlyTheiaExtensions: true,
suppress
});
}
Expand All @@ -237,6 +265,7 @@ async function theiaCli(): Promise<void> {
skipHoisted: boolean,
skipUniqueness: boolean,
skipSingleTheiaVersion: boolean,
onlyTheiaExtensions: boolean,
suppress: boolean
}>({
command: 'check:dependencies',
Expand Down Expand Up @@ -280,6 +309,12 @@ async function theiaCli(): Promise<void> {
boolean: true,
default: false
},
'only-theia-extensions': {
alias: 'o',
describe: 'Only check dependencies which are Theia extensions',
boolean: true,
default: false
},
'suppress': {
alias: 's',
describe: 'Suppress exiting with failure code',
Expand All @@ -294,6 +329,7 @@ async function theiaCli(): Promise<void> {
skipHoisted,
skipUniqueness,
skipSingleTheiaVersion,
onlyTheiaExtensions,
suppress
}) => {
checkDependencies({
Expand All @@ -303,6 +339,7 @@ async function theiaCli(): Promise<void> {
skipHoisted,
skipUniqueness,
skipSingleTheiaVersion,
onlyTheiaExtensions,
suppress
});
}
Expand Down

0 comments on commit dab0aad

Please sign in to comment.