generated at
DenoからGoogle Apps Scriptの型定義ファイルを使えるか試す
2021-07-11
型定義ファイルはこちら
どうやって使えばいいかな?
18:34:39 そのまま使うのは無理そう
型定義ファイルのmodule解決の方法がNode用になっている
対策
Deno用に書き換える
/// <reference path=" /// <reference path="./ に置換すればいけそう
こんな感じのscriptを組む
octokit.jsを使ってdownloadする
sh
deno 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.ts
import {download} from "./fetch.js"; download();
fetch.d.ts
export function download(): Promise<void>;
fetch.js
import { 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="./ に置換する
sh
deno 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.ts
import {convert} from "./convert.ts"; convert();
convert.ts
import { 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は完成した
sh
deno 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.ts
import {download} from "./fetch.js"; import {convert} from "./convert.ts"; await download(); await convert();
生成した型定義ファイルは、Denoで型定義ファイルを読み込#60eacf871280f00000a150a9を使って読み込む
failesm.shなどで型定義ファイルをbundleできないか試す
18:36:18 だめでした
エラーが出てしまった
20:53:02 githubに型定義ファイルを置いた
LICENSEの書き方わからない……
20:57:46 あれ?GitHubから直接importしようとすると読み込めない?
もしかして<reference path=>はURLを解決してくれないのか?
まじかー……takker
流石に全てのnamed Importを書くのは大変だぞ……
自動置換では対処できない
副作用付きimportでなんとかなるかな?
なんとかならなかった
global変数は副作用付きimportでなんとかなるけど、それ以外の型はnamed importしないと無理
21:24:33 @deno-typesを使ってもだめだった……
毎回毎回localに型定義をdownloadするか、Denoから読み込めるような型定義ファイルを自作するしかないなあ……
2021-07-13 19:20:01 Deno用の型定義ファイルを作っている事例を見つけた
もうメンテはされていない模様
2021-07-25
14:04:20 <reference types=>を使えばいけるっぽい?
やってみる
bash
deno 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.ts
import {download} from "./fetch.js"; import {convert} from "./convert2.ts"; await download(); await convert();
convert2.ts
import { 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" />') ); } }

#2021-08-10 09:35:08
#2021-07-13 19:21:31
#2021-07-11 18:24:20