2019-09-24 19:11:08 +02:00
/// <reference types="node"/>
import { normalize , dirname , join } from "path" ;
import { readFileSync , writeFileSync , unlinkSync , existsSync } from "fs" ;
import assert = require ( "assert" ) ;
import { execSync } from "child_process" ;
const args = process . argv . slice ( 2 ) ;
/ * *
* A minimal description for a parsed package . json object .
* /
interface PackageJson {
name : string ;
bin : { } ;
main : string ;
2019-09-24 20:26:35 +02:00
scripts : {
prepare : string
postpublish : string
}
2019-09-24 19:11:08 +02:00
}
function main ( ) : void {
if ( args . length < 1 ) {
console . log ( "Usage:" ) ;
console . log ( "\tnode configureTSCBuild.js <package.json location>" ) ;
return ;
}
// Acquire the version from the package.json file and modify it appropriately.
const packageJsonFilePath = normalize ( args [ 0 ] ) ;
const packageJsonValue : PackageJson = JSON . parse ( readFileSync ( packageJsonFilePath ) . toString ( ) ) ;
// Remove the bin section from the current package
delete packageJsonValue . bin ;
2019-09-24 20:26:35 +02:00
// We won't be running eslint which would run before publishing
delete packageJsonValue . scripts . prepare ;
// No infinite loops
delete packageJsonValue . scripts . postpublish ;
2019-09-24 19:11:08 +02:00
// Set the new name
2019-09-26 19:23:19 +02:00
packageJsonValue . name = "@typescript/language-services" ;
2019-09-24 19:11:08 +02:00
writeFileSync ( packageJsonFilePath , JSON . stringify ( packageJsonValue , /*replacer:*/ undefined , /*space:*/ 4 ) ) ;
// Remove the files which aren't use when just using the API
const toRemove = [
// JS Files
"tsserver.js" ,
"tsserverlibrary.js" ,
"typescriptServices.js" ,
"typingsInstaller.js" ,
"tsc.js" ,
// DTS files
"typescriptServices.d.ts" ,
"tsserverlibrary.d.ts"
] ;
2019-09-24 20:26:35 +02:00
// Get a link to the main dependency JS file
2019-09-24 19:11:08 +02:00
const lib = join ( dirname ( packageJsonFilePath ) , packageJsonValue . main ) ;
const libPath = dirname ( lib ) ;
2019-09-24 20:26:35 +02:00
// Remove the sibling JS large files referenced above
2019-09-24 19:11:08 +02:00
toRemove . forEach ( file = > {
const path = join ( libPath , file ) ;
if ( existsSync ( path ) ) unlinkSync ( path ) ;
} ) ;
2019-09-24 20:26:35 +02:00
// Remove VS-specific localization keys
execSync ( "rm -rf loc" , { cwd : dirname ( packageJsonFilePath ) } ) ;
// Remove runnable file reference
execSync ( "rm -rf bin" , { cwd : dirname ( packageJsonFilePath ) } ) ;
2019-09-24 19:11:08 +02:00
///////////////////////////////////
// This section verifies that the build of TypeScript compiles and emits
2019-09-26 19:23:19 +02:00
const ts = require ( lib ) ;
2019-09-24 19:11:08 +02:00
const source = "let x: string = 'string'" ;
2019-09-26 19:23:19 +02:00
const results = ts . transpileModule ( source , {
2019-09-24 19:11:08 +02:00
compilerOptions : { module : ts.ModuleKind.CommonJS }
} ) ;
assert ( results . outputText . trim ( ) === "var x = 'string';" , ` Running typescript with ${ packageJsonValue . name } did not return the expected results, got: ${ results . outputText } ` ) ;
}
main ( ) ;