DenoからGoogle Apps Scriptの型定義ファイルを使えるか試す
2021-07-11
型定義ファイルはこちら
どうやって使えばいいかな?
18:34:39 そのまま使うのは無理そう
型定義ファイルのmodule解決の方法がNode用になっている
対策
Deno用に書き換える
/// <reference path="
を /// <reference path="./
に置換すればいけそう
こんな感じのscriptを組む
shdeno run --unstable --allow-net=api.github.com,raw.githubusercontent.com --allow-write=./ --allow-read=./ -r=https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/test.js https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/test.js TOKEN
test.tsimport {download} from "./fetch.js";
download();
fetch.d.tsexport function download(): Promise<void>;
fetch.jsimport { Octokit } from "https://esm.sh/octokit@1.1.0?no-check";
import { exists } from "https://deno.land/std@0.100.0/fs/mod.ts";
// 認証
// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ auth: Deno.args[0] });
export async function download() {
await expand('types/google-apps-script');
}
async function expand(path) {
const options = {
owner: 'DefinitelyTyped',
repo: 'DefinitelyTyped',
ref: 'master',
};
const {data} = await octokit.rest.repos.getContent({
path,
...options,
});
for (const content of data) {
if (content.type === 'dir') {
if (!(await exists(content.name))) {
await Deno.mkdir(content.name);
}
await Deno.chdir(content.name);
await expand(content.path);
await Deno.chdir('../');
continue;
}
const res = await fetch(content.download_url);
const code = await res.text();
await Deno.writeTextFile(content.name, code);
}
}
2. /// <reference path="
を /// <reference path="./
に置換する
shdeno run --unstable --allow-write=./ --allow-read=./ -r=https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/convert.ts https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/test2.ts
test2.tsimport {convert} from "./convert.ts";
convert();
convert.tsimport { expandGlob } from "https://deno.land/std@0.100.0/fs/mod.ts";
export async function convert() {
for await (const file of expandGlob('**/*.ts')) {
const code = await Deno.readTextFile(file.path);
await Deno.writeTextFile(
file.path,
//code.replaceAll(`/// <reference path="`, `/// <reference path="./`)
code.replace(/\/\/\/\s*<reference\s*path="([^"]+)"\s*\/>/g, '/// <reference path="./$1" />\n// @deno-types="./$1"')
);
}
}
19:59:14 codeは完成した
shdeno run --unstable --allow-net=api.github.com,raw.githubusercontent.com --allow-write=./ --allow-read=./ https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/script.ts TOKEN
script.tsimport {download} from "./fetch.js";
import {convert} from "./convert.ts";
await download();
await convert();

esm.shなどで型定義ファイルをbundleできないか試す
18:36:18 だめでした
エラーが出てしまった
20:53:02 githubに型定義ファイルを置いた
20:57:46 あれ?GitHubから直接importしようとすると読み込めない?
まじかー……

流石に全てのnamed Importを書くのは大変だぞ……
自動置換では対処できない
副作用付きimportでなんとかなるかな?
なんとかならなかった
global変数は副作用付きimportでなんとかなるけど、それ以外の型はnamed importしないと無理
毎回毎回localに型定義をdownloadするか、Denoから読み込めるような型定義ファイルを自作するしかないなあ……
もうメンテはされていない模様
2021-07-25
やってみる
bashdeno run --unstable --allow-net=api.github.com,raw.githubusercontent.com --allow-write=./ --allow-read=./ https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/script2.ts TOKEN
script2.tsimport {download} from "./fetch.js";
import {convert} from "./convert2.ts";
await download();
await convert();
convert2.tsimport { expandGlob } from "https://deno.land/std@0.100.0/fs/mod.ts";
export async function convert() {
for await (const file of expandGlob('**/*.ts')) {
const code = await Deno.readTextFile(file.path);
await Deno.writeTextFile(
file.path,
//code.replaceAll(`/// <reference path="`, `/// <reference path="./`)
code.replace(/\/\/\/\s*<reference\s*path="([^"]+)"\s*\/>/g, '/// <reference types="./$1" />')
);
}
}