Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use vscode API instead of Node module #19

Merged
merged 2 commits into from
May 28, 2021

Conversation

takahashim
Copy link
Contributor

  • use vscode.workspace.fs API
  • pathes are vscode.Uri, not string
  • use child_process.execFile instead of child_process.exec

#18 についての修正で、extension.tsを置き換えてみました。
PDFが生成できることまでは確認できました。
また、パスのエスケープが大変そうだったので、vivliostyleのコマンド実行のところをexecFileを使うように修正しています。

* use `vscode.workspace.fs` API
* pathes are `vscode.Uri`, not `string`
* use `child_process.execFile` instead of `child_process.exec`
@@ -17,7 +15,7 @@ const output = vscode.window.createOutputChannel("Novel");
let html: Buffer;

//コマンド登録
export function activate(context: vscode.ExtensionContext) {
export function activate(context: vscode.ExtensionContext): void {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここはeslint対策でvoidを明示させてみました

@@ -29,7 +27,10 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(controller);
context.subscriptions.push(characterCounter);

html = fs.readFileSync(path.join(context.extensionPath, 'htdocs', 'index.html'));
const fileUri = vscode.Uri.joinPath(context.extensionUri, 'htdocs', 'index.html');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vscode.workspace.fsはパス等の指定にstringではなくvscode.Uriを取るようなので、それに合わせてjoinPathextensionUriなどに置き換えました。

Comment on lines +31 to +33
vscode.workspace.fs.readFile(fileUri).then((data) => {
html = Buffer.from(data);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer型ではなくバイナリ型(Uint8Array)が返ってくるので変換しています。

//https://docs.vivliostyle.org/#/ja/vivliostyle-cli
output.appendLine(`saving pdf to ${vivlioCommand}`);
cp.exec(vivlioCommand, (err, stdout, stderr) => {
if (!vscode.workspace.workspaceFolders) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vscode.workspace.workspaceFoldersがnullだったときのエラー処理を追加してeslintの警告に対応しています

Comment on lines +184 to +186
const folderUri = vscode.workspace.workspaceFolders[0].uri;
const myPath = vscode.Uri.joinPath(folderUri, 'publish.html');
const myWorkingDirectory = folderUri;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この辺もみんなvscode.Uriにしています

Comment on lines +187 to +191
const vivlioCommand = 'vivliostyle';
const vivlioSubCommand = 'build';

output.appendLine(`starting to publish: ${myPath}`);
const vivlioParams = [vivlioSubCommand, myPath.fsPath, '-o', vscode.Uri.joinPath(myWorkingDirectory, "output.pdf").fsPath];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この辺りについては、execではなくexecFileを使うようにしたための変更で、引数を配列で渡せるようになるため、vivlioCommandは実行ファイルだけにして、vivlioParamsという配列を追加しています。

output.appendLine(`starting to publish: ${vivlioCommand} ${vivlioParams}`);
const myHtmlBinary = Buffer.from(myHtml, 'utf8');

vscode.workspace.fs.writeFile(myPath, myHtmlBinary).then(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APIが代わりpromiseが渡されるようになったので、then()で受けるようにしています。


vscode.workspace.fs.writeFile(myPath, myHtmlBinary).then(() => {
output.appendLine(`saving pdf to ${vivlioCommand}`);
cp.execFile(vivlioCommand, vivlioParams, (err, stdout, stderr) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここがexecからexecFileに変更されています

@ttrace
Copy link
Owner

ttrace commented May 28, 2021

ありがとうございます。マージします。

@ttrace ttrace merged commit 6fed5f2 into ttrace:master May 28, 2021
@takahashim takahashim deleted the use-vscode-fs branch May 29, 2021 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants