Skip to content

Commit

Permalink
Fix uninstall script (#2385)
Browse files Browse the repository at this point in the history
Co-authored-by: Roberto Huertas <roberto.huertas@outlook.com>
  • Loading branch information
JimiC and robertohuertasm committed Apr 5, 2020
1 parent 28f6171 commit 8665737
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/constants/index.ts
Expand Up @@ -8,6 +8,7 @@ export const constants = {
version: manifest.version,
customIconFolderName: 'vsicons-custom-icons',
distEntryFilename: 'vscode-icons.bundle.js',
uninstallEntryFilename: 'uninstall.bundle.js',
outDirName: 'out',
distDirName: 'dist',
srcDirName: 'src',
Expand Down
37 changes: 33 additions & 4 deletions src/iconsManifest/iconsGenerator.ts
Expand Up @@ -112,21 +112,36 @@ export class IconsGenerator implements models.IIconsGenerator {
}

private async updatePackageJson(): Promise<void> {
const oldMainPath = manifest.main;
const oldIconsThemesPath = manifest.contributes.iconThemes[0].path;
const sourceDirRelativePath = await Utils.getRelativePath(
ConfigManager.rootDir,
ConfigManager.sourceDir,
);

const entryFilename = constants.environment.production
? constants.extension.distEntryFilename
: '';
const entryPath = `${sourceDirRelativePath}${entryFilename}`;
const iconsDirRelativePath = `${sourceDirRelativePath}${constants.iconsManifest.filename}`;
const oldMainPath = manifest.main;
const mainPathChanged = oldMainPath && oldMainPath !== entryPath;

const uninstallEntryFilename = constants.environment.production
? constants.extension.uninstallEntryFilename
: 'uninstall.js';
const uninstallEntryPath = `node ./${sourceDirRelativePath}${uninstallEntryFilename}`;
const oldUninstallScriptPath = manifest.scripts['vscode:uninstall'];
const uninstallScriptPathChanged =
oldUninstallScriptPath && oldUninstallScriptPath !== uninstallEntryPath;

const iconsDirRelativePath = `${sourceDirRelativePath}${constants.iconsManifest.filename}`;
const oldIconsThemesPath = manifest.contributes.iconThemes[0].path;
const iconThemePathChanged =
oldIconsThemesPath && oldIconsThemesPath !== iconsDirRelativePath;
if (!iconThemePathChanged && !mainPathChanged) {

if (
!iconThemePathChanged &&
!mainPathChanged &&
!uninstallScriptPathChanged
) {
return;
}
const replacer = (rawText: string[]): string[] => {
Expand Down Expand Up @@ -156,6 +171,20 @@ export class IconsGenerator implements models.IIconsGenerator {
`[${constants.extension.name}] Entrypoint in 'package.json' updated`,
);
}
// update 'vscode:uninstall' script
predicate = (line: string): boolean =>
line.includes('"vscode:uninstall"');
lineIndex = rawText.findIndex(predicate);
if (lineIndex > -1) {
rawText[lineIndex] = rawText[lineIndex].replace(
manifest.scripts['vscode:uninstall'],
uninstallEntryPath,
);
// eslint-disable-next-line no-console
console.info(
`[${constants.extension.name}] Script 'vscode:uninstall' in 'package.json' updated`,
);
}
return rawText;
};
try {
Expand Down
54 changes: 54 additions & 0 deletions test/iconsManifest/iconsGenerator/iconsGenerator.test.ts
Expand Up @@ -374,6 +374,25 @@ describe('IconsGenerator: tests', function () {
expect(updateFileStub.called).to.be.false;
});
});

context(`when the 'vscode:uninstall' script`, function () {
it(`does NOT exist`, async function () {
const originalValue = manifest.scripts['vscode:unistall'];
delete manifest.scripts['vscode:unistall'];

await iconsGenerator.persist(iconsManifest, true);

manifest.scripts['vscode:unistall'] = originalValue;

expect(updateFileStub.called).to.be.false;
});

it(`does NOT need to be changed`, async function () {
await iconsGenerator.persist(iconsManifest, true);

expect(updateFileStub.called).to.be.false;
});
});
});

context(`updated`, function () {
Expand Down Expand Up @@ -495,6 +514,41 @@ describe('IconsGenerator: tests', function () {
});
});
});

context(`and the 'scripts' property gets`, function () {
context(`NOT updated`, function () {
it(`when it can NOT be found`, async function () {
updateFileStub.callsArgWith(1, ['']).resolves();

await iconsGenerator.persist(iconsManifest, true);

expect(updateFileStub.calledOnce).to.be.true;
expect(infoStub.calledOnce).to.be.true;
expect(
infoStub.calledWithExactly(
`[${constants.extension.name}] Icons manifest file successfully generated!`,
),
).to.be.true;
});
});

it(`updated`, async function () {
getRelativePathStub.resolves('some/path/');
updateFileStub
.callsArgWith(1, [`"main":"some/path/"\n"path":""\n"scripts":{"vscode:uninstall":""}`])
.resolves();

await iconsGenerator.persist(iconsManifest, true);

expect(updateFileStub.calledOnce).to.be.true;
expect(infoStub.callCount).to.equal(4);
expect(
infoStub.calledWithExactly(
`[${constants.extension.name}] Script 'vscode:uninstall' in 'package.json' updated`,
),
).to.be.true;
});
});
});
});
});
Expand Down
73 changes: 42 additions & 31 deletions webpack.config.ts
Expand Up @@ -3,36 +3,47 @@ import { resolve } from 'path';
import { CliConfigOptions, Configuration } from 'webpack';
import { constants } from './out/src/constants';

export default (
_env: string | Record<string, boolean | number | string>,
argv: CliConfigOptions,
): Configuration => {
const config: Configuration = {
mode: argv.mode,
target: 'node',
context: resolve(__dirname, 'out'),
output: {
path: resolve(__dirname, 'dist/src'),
filename: constants.extension.distEntryFilename,
libraryTarget: 'commonjs2',
},
node: {
__dirname: false,
__filename: false,
},
externals: {
// The vscode-module is created on-the-fly and must be excluded.
// Add other modules that cannot be webpack'ed.
// 📖 -> https://webpack.js.org/configuration/externals/
vscode: 'commonjs vscode',
},
};

const getConfig = (argv: CliConfigOptions): Configuration => ({
context: resolve(__dirname, 'out'),
// development mode only
if (config.mode === 'development') {
config.devtool = 'source-map';
config.output.devtoolModuleFilenameTemplate = '../../[resource-path]';
devtool: argv.mode === 'development' ? 'source-map' : false,
externals: {
// The vscode-module is created on-the-fly and must be excluded.
// Add other modules that cannot be webpack'ed.
// 📖 -> https://webpack.js.org/configuration/externals/
vscode: 'commonjs vscode',
},
mode: argv.mode,
node: {
__dirname: false,
__filename: false,
},
output: {
path: resolve(__dirname, 'dist/src'),
libraryTarget: 'commonjs2',
// development mode only
devtoolModuleFilenameTemplate:
argv.mode === 'development' ? '../../[resource-path]' : '',
},
target: 'node',
});

export default [
(
_env: string | Record<string, boolean | number | string>,
argv: CliConfigOptions,
): Configuration => {
const config: Configuration = getConfig(argv);
config.output.filename = constants.extension.distEntryFilename;
return config;
},
(
_env: string | Record<string, boolean | number | string>,
argv: CliConfigOptions,
): Configuration => {
const config: Configuration = getConfig(argv);
config.entry = './src/uninstall.js';
config.output.filename = constants.extension.uninstallEntryFilename;
return config;
}
return config;
};
},
];

0 comments on commit 8665737

Please sign in to comment.