-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Conversation
* 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 { |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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を取るようなので、それに合わせてjoinPath
やextensionUri
などに置き換えました。
vscode.workspace.fs.readFile(fileUri).then((data) => { | ||
html = Buffer.from(data); | ||
}); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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の警告に対応しています
const folderUri = vscode.workspace.workspaceFolders[0].uri; | ||
const myPath = vscode.Uri.joinPath(folderUri, 'publish.html'); | ||
const myWorkingDirectory = folderUri; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この辺もみんなvscode.Uriにしています
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]; |
There was a problem hiding this comment.
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(() => { |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここがexecからexecFileに変更されています
ありがとうございます。マージします。 |
vscode.workspace.fs
APIvscode.Uri
, notstring
child_process.execFile
instead ofchild_process.exec
#18 についての修正で、extension.tsを置き換えてみました。
PDFが生成できることまでは確認できました。
また、パスのエスケープが大変そうだったので、vivliostyleのコマンド実行のところを
execFile
を使うように修正しています。