Skip to content

Instantly share code, notes, and snippets.

@ovrmrw
Last active July 16, 2016 12:00
Show Gist options
  • Save ovrmrw/427a3961c27292e27493c8e056c2489b to your computer and use it in GitHub Desktop.
Save ovrmrw/427a3961c27292e27493c8e056c2489b to your computer and use it in GitHub Desktop.
Copy *.ts and *.d.ts files from ./jspm_packages/npm/ to ./node_modules/@types/
/*
Copy *.ts and *.d.ts files from ./jspm_packages/npm/ to ./node_modules/@types/
This tool is for TypeScript 2.0.
*/
const lodash = require('lodash');
const fs = require('fs-extra');
const path = require('path');
const root = path.resolve();
require('./jspm_packages/system.src'); // SystemJS
require('./jspm.config'); // jspm config file
console.log(SystemJS.map);
lodash.forEach(SystemJS.map, (value, key) => {
const npm = value.split(':')[0];
if (npm === 'npm') {
console.log(key); // @angular/core
const path = value.replace(':', '/');
console.log(path); // npm/@angular/[email protected]
fs.copySync(root + '/jspm_packages/' + path, root + '/node_modules/@types/' + key, { filter: /\.ts/ });
}
});
@Quramy
Copy link

Quramy commented Jul 15, 2016

下記のように書くことで、tsconfig.jsonに変換できました. 生成したtsconfig.json使ってcompile通せましたー。

require('./jspm_packages/system.src'); // SystemJS
require('./config'); // jspm config file

const tsconfig = {
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2015",
    "noImplicitAny": false,
    "sourceMap": false
  },
  "exclude": [
    "node_modules",
    "jspm_packages"
  ]
};

const baseUrl = SystemJS.baseURL.replace(/^file:\/\/\//, "");

const paths = {};
Object.keys(SystemJS.map).forEach(key => {
  const value = SystemJS.map[key];

  // SystemJS.pathsを読んで、解決すべきだけど、手抜き
  const flatten = value.replace(/npm:/, "jspm_packages/npm/");
  paths[key] = [flatten];
});

const compilerOptions = Object.assign({}, tsconfig.compilerOptions, {baseUrl, paths});
console.log(JSON.stringify(Object.assign({}, tsconfig, {compilerOptions}), null, 2));

// 以下のtsconfg.jsonが生成され, jspm配下のrxjsをimport出来るようになる
// {
//   "compilerOptions": {
//     "module": "commonjs",
//     "target": "es2015",
//     "noImplicitAny": false,
//     "sourceMap": false,
//     "baseUrl": "",
//     "paths": {
//       "rxjs": [
//         "jspm_packages/npm/[email protected]"
//       ]
//     }
//   },
//   "exclude": [
//     "node_modules",
//     "jspm_packages"
//   ]
// }
//

@ovrmrw
Copy link
Author

ovrmrw commented Jul 15, 2016

Greate work!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment