Skip to content

Commit c5e78df

Browse files
committed
chore: update types
1 parent 960acef commit c5e78df

File tree

3 files changed

+99
-66
lines changed

3 files changed

+99
-66
lines changed

src/pynsist.ts

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Dependencies
2-
import { dirname } from 'path';
3-
import { getConfig } from 'vscode-get-config';
4-
import { spawn } from 'child_process';
5-
import { workspace, window } from 'vscode';
2+
import { dirname } from "path";
3+
// @ts-expect-error TODO Fix package
4+
import { getConfig } from "vscode-get-config";
5+
import { spawn } from "child_process";
6+
import { workspace, window } from "vscode";
67

78
// Package Components
8-
import { clearOutput, detectOutput, getPath, pathWarning, runInstaller, sanitize } from './util';
9+
import {
10+
clearOutput,
11+
detectOutput,
12+
getPath,
13+
pathWarning,
14+
runInstaller,
15+
sanitize,
16+
} from "./util";
917

10-
const channel = window.createOutputChannel('pynsist');
18+
const channel = window.createOutputChannel("pynsist");
1119

1220
/*
1321
* Requires pynsist
@@ -17,88 +25,110 @@ const channel = window.createOutputChannel('pynsist');
1725
async function generate(runMakensis: boolean): Promise<void> {
1826
await clearOutput(channel);
1927

20-
const doc = window.activeTextEditor.document;
28+
const doc = window?.activeTextEditor?.document;
29+
30+
if (!doc) {
31+
console.error("[idleberg.applescript] Document not found");
32+
return;
33+
}
34+
2135
const scope = doc.languageId;
2236

23-
if (scope !== 'properties' && doc.fileName === 'installer.cfg') {
24-
channel.appendLine('This command is only available for Pynsist Configuration files');
37+
if (scope !== "properties" && doc.fileName === "installer.cfg") {
38+
channel.appendLine(
39+
"This command is only available for Pynsist Configuration files",
40+
);
2541
return;
2642
}
2743

28-
const { showNotifications } = await getConfig('pynsist');
44+
const { showNotifications } = await getConfig("pynsist");
2945

3046
doc.save().then(async () => {
3147
await getPath()
32-
.then(sanitize)
48+
.then((path) => sanitize(path.toString()))
3349
.then((pathToPynsist: string) => {
34-
if (typeof pathToPynsist === 'undefined' || pathToPynsist === null) {
35-
return window.showErrorMessage('No valid `pynsist` was specified in your config');
50+
if (typeof pathToPynsist === "undefined" || pathToPynsist === null) {
51+
return window.showErrorMessage(
52+
"No valid `pynsist` was specified in your config",
53+
);
3654
}
3755

3856
const defaultArguments: Array<string> = [doc.fileName];
3957

4058
if (runMakensis === false) {
41-
defaultArguments.push('--no-makensis');
59+
defaultArguments.push("--no-makensis");
4260
}
4361

4462
// Let's build
4563
const pynsist = spawn(pathToPynsist, defaultArguments);
4664

4765
const scriptPath: string = dirname(doc.fileName);
48-
let outScript = '';
49-
let outFile = '';
66+
let outScript = "";
67+
let outFile = "";
5068

51-
pynsist.stdout.on('data', (line: string) => {
69+
pynsist.stdout.on("data", (line: string) => {
5270
channel.appendLine(line.toString().trim());
5371
});
5472

5573
// pynsist currently outputs to stderr only (v1.12)
56-
pynsist.stderr.on('data', async (line: string) => {
74+
pynsist.stderr.on("data", async (line: string) => {
5775
channel.appendLine(line.toString().trim());
5876

59-
if (outScript === '') {
77+
if (outScript === "") {
6078
outScript = await detectOutput(scriptPath, line, {
61-
string: 'Writing NSI file to ',
79+
string: "Writing NSI file to ",
6280
regex: /Writing NSI file to (.*)\r?\n/g,
6381
});
6482
}
6583

66-
if (outFile === '' && runMakensis === true) {
84+
if (outFile === "" && runMakensis === true) {
6785
outFile = await detectOutput(scriptPath, line, {
68-
string: 'Installer written to ',
86+
string: "Installer written to ",
6987
regex: /Installer written to (.*)\r?\n/g,
7088
});
7189
}
7290
});
7391

74-
pynsist.on('close', async (code) => {
92+
pynsist.on("close", async (code) => {
7593
if (code === 0) {
7694
if (showNotifications) {
7795
if (runMakensis === true) {
7896
window
79-
.showInformationMessage('Successfully compiled installer', 'Run Installer', 'Open Script')
97+
.showInformationMessage(
98+
"Successfully compiled installer",
99+
"Run Installer",
100+
"Open Script",
101+
)
80102
.then(async (choice) => {
81-
if (choice === 'Run Installer') {
103+
if (choice === "Run Installer") {
82104
await runInstaller(outFile);
83-
} else if (choice === 'Open Script') {
105+
} else if (choice === "Open Script") {
84106
workspace.openTextDocument(outScript).then((doc) => {
85107
window.showTextDocument(doc);
86108
});
87109
}
88110
});
89111
} else {
90-
window.showInformationMessage('Successfully generated script', 'Open Script').then((choice) => {
91-
if (choice === 'Open Script') {
92-
workspace.openTextDocument(outScript).then((doc) => {
93-
window.showTextDocument(doc);
94-
});
95-
}
96-
});
112+
window
113+
.showInformationMessage(
114+
"Successfully generated script",
115+
"Open Script",
116+
)
117+
.then((choice) => {
118+
if (choice === "Open Script") {
119+
workspace.openTextDocument(outScript).then((doc) => {
120+
window.showTextDocument(doc);
121+
});
122+
}
123+
});
97124
}
98125
}
99126
} else {
100127
channel.show(true);
101-
if (showNotifications) window.showErrorMessage('Something went wrong. See the output for details.');
128+
if (showNotifications)
129+
window.showErrorMessage(
130+
"Something went wrong. See the output for details.",
131+
);
102132
}
103133
});
104134
})

src/task.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
1-
// Dependencies
2-
import { getConfig } from 'vscode-get-config';
3-
import { join } from 'path';
4-
import { mkdir, writeFile } from 'fs';
5-
import { workspace, window } from 'vscode';
6-
7-
// Package Components
8-
import { getPath, sanitize } from './util';
1+
// @ts-expect-error TODO Fix package
2+
import { getConfig } from "vscode-get-config";
3+
import { join, resolve } from "path";
4+
import { mkdir, writeFile } from "fs";
5+
import { workspace, window } from "vscode";
6+
import { getPath, sanitize } from "./util";
97

108
async function createTask(): Promise<void> {
11-
if (typeof workspace.rootPath === 'undefined' || workspace.rootPath === null) {
9+
if (
10+
typeof workspace.workspaceFolders === "undefined" ||
11+
workspace.workspaceFolders.length < 1
12+
) {
1213
window.showErrorMessage(
13-
'Task support is only available when working on a workspace folder. It is not available when editing single files.',
14+
"Task support is only available when working on a workspace folder. It is not available when editing single files.",
1415
);
1516
return;
1617
}
1718

1819
getPath()
19-
.then(sanitize)
20+
.then((path) => sanitize(path.toString()))
2021
.then(async (pathToPynsist: string) => {
2122
const taskFile: unknown = {
22-
version: '2.0.0',
23+
version: "2.0.0",
2324
tasks: [
2425
{
25-
label: 'pynsist: Compile Installer',
26-
type: 'shell',
26+
label: "pynsist: Compile Installer",
27+
type: "shell",
2728
command: `${pathToPynsist}`,
28-
args: ['${file}'],
29-
group: 'build',
29+
args: ["${file}"],
30+
group: "build",
3031
},
3132
{
32-
label: 'pynsist: Generate Script',
33-
type: 'shell',
33+
label: "pynsist: Generate Script",
34+
type: "shell",
3435
command: `${pathToPynsist}`,
35-
args: ['--no-makensis', '${file}'],
36-
group: 'build',
36+
args: ["--no-makensis", "${file}"],
37+
group: "build",
3738
},
3839
],
3940
};
4041

4142
const jsonString: string = JSON.stringify(taskFile, null, 2);
42-
const dotFolder: string = join(workspace.rootPath, '/.vscode');
43-
const buildFile: string = join(dotFolder, 'tasks.json');
43+
const dotFolder: string = resolve(
44+
workspace.workspaceFolders![0]!.uri.fsPath,
45+
".vscode",
46+
);
47+
const buildFile: string = join(dotFolder, "tasks.json");
4448

4549
mkdir(dotFolder, () => {
4650
// ignore errors for now
@@ -49,7 +53,8 @@ async function createTask(): Promise<void> {
4953
window.showErrorMessage(error.toString());
5054
return;
5155
}
52-
if ((await getConfig('pynsist.alwaysOpenBuildTask')) === false) return;
56+
if ((await getConfig("pynsist.alwaysOpenBuildTask")) === false)
57+
return;
5358

5459
// Open tasks.json
5560
workspace.openTextDocument(buildFile).then((doc) => {

src/util.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Dependencies
22
import { constants, promises as fs } from "fs";
3+
// @ts-expect-error TODO Fix package
34
import { getConfig } from "vscode-get-config";
45
import { join } from "path";
56
import { platform } from "os";
67
import { spawn } from "child_process";
78
import { window, type OutputChannel } from "vscode";
89
import open from "open";
10+
import which from "which";
911

1012
async function clearOutput(channel: OutputChannel): Promise<void> {
1113
const { alwaysShowOutput } = await getConfig("pynsist");
@@ -58,18 +60,18 @@ async function getPath(): Promise<string | number> {
5860
return resolve(pathToPynsist);
5961
}
6062

61-
const which = spawn(this.which(), ["pynsist"]);
63+
const cp = spawn("pynsist");
6264

63-
which.stdout.on("data", (data) => {
65+
cp.stdout.on("data", (data) => {
6466
console.log("Using pynsist path detected on file system: " + data);
6567
return resolve(data);
6668
});
6769

68-
which.on("error", (errorMessage) => {
70+
cp.on("error", (errorMessage) => {
6971
console.error({ errorMessage: errorMessage });
7072
});
7173

72-
which.on("close", (code) => {
74+
cp.on("close", (code) => {
7375
if (code !== 0) {
7476
return reject(code);
7577
}
@@ -108,14 +110,10 @@ async function runInstaller(outFile: string): Promise<void> {
108110
}
109111
}
110112

111-
function sanitize(response: unknown): string {
113+
function sanitize(response: string): string {
112114
return response.toString().trim();
113115
}
114116

115-
function which(): string {
116-
return platform() === "win32" ? "where" : "which";
117-
}
118-
119117
export {
120118
clearOutput,
121119
detectOutput,

0 commit comments

Comments
 (0)