Skip to content

Commit 6672073

Browse files
committed
feat(package) we will automatically determine package.json for you
1 parent 4113cf9 commit 6672073

File tree

5 files changed

+52
-46
lines changed

5 files changed

+52
-46
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ JavaScript developers can now just open a `.ts` file and start hacking away like
2828
* Compile on save
2929
* Project Context Support (`tsconfig.json`)
3030
* Project Build Support
31+
* `package.json` Support
3132
* Format code
3233
* Goto Declaration
3334
* Find References
@@ -81,6 +82,22 @@ Shortcut: `F6`. If there are any errors they are shown as well.
8182

8283
![](https://raw.githubusercontent.com/TypeStrong/atom-typescript/master/docs/screens/build%20errors.png)
8384

85+
## `package.json` Support
86+
Where a sample package.json looks like:
87+
```json
88+
{
89+
"name": "awesome",
90+
"main": "./dist/foo.js",
91+
"typescript": {
92+
"definition": "./definition/awesome.d.ts"
93+
}
94+
}
95+
```
96+
97+
We would generate a `definition/awesome.d.ts` file for you so that other TypeScript projects can do a simple `require('awesome')`.
98+
99+
Also note that any node modules that ship with `typescript.definition` that you import will automatically get type inference / completion.
100+
84101
## Format Code
85102
Shortcut : `ctrl+alt+l` or `cmd+alt+l`. Will format just the selection if you have something selected otherwise it will format the entire file.
86103

dist/main/tsconfig/tsconfig.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,21 @@ function getProjectSync(pathOrSrcFile) {
206206
}
207207
}
208208
projectSpec.files = projectSpec.files.map(function (file) { return path.resolve(projectFileDirectory, file); });
209-
var packagePath = projectSpec.package;
210209
var package = null;
211-
if (packagePath) {
212-
var packageJSONPath = getPotentiallyRelativeFile(projectFileDirectory, packagePath);
213-
var parsedPackage = JSON.parse(fs.readFileSync(packageJSONPath).toString());
214-
package = {
215-
main: parsedPackage.main,
216-
name: parsedPackage.name,
217-
directory: path.dirname(packageJSONPath),
218-
definition: parsedPackage.typescript && parsedPackage.typescript.definition
219-
};
210+
try {
211+
var packagePath = travelUpTheDirectoryTreeTillYouFind(projectFileDirectory, 'package.json');
212+
if (packagePath) {
213+
var packageJSONPath = getPotentiallyRelativeFile(projectFileDirectory, packagePath);
214+
var parsedPackage = JSON.parse(fs.readFileSync(packageJSONPath).toString());
215+
package = {
216+
main: parsedPackage.main,
217+
name: parsedPackage.name,
218+
directory: path.dirname(packageJSONPath),
219+
definition: parsedPackage.typescript && parsedPackage.typescript.definition
220+
};
221+
}
222+
}
223+
catch (ex) {
220224
}
221225
var project = {
222226
compilerOptions: {},

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ We only plan strictly document the breaking changes. The rest is optional.
99
# v2
1010
* New default shortcuts for `build` : `F6` an `goto definition`: `F12`. Because I don't want to mess with your atom defaults and a major use base is VS users. [Link #145](https://github.com/TypeStrong/atom-typescript/issues/145)
1111

12+
# v3
13+
* We will now resolve a parent `package.json` for you *automatically* so that its *one less thing you need to configure*. :rose:
14+
1215
# Planned
1316
* No breaking changes yet.

docs/tsconfig.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ i.e. an empty JSON file at the *root* of your project :heart: This will be suffi
1313
* [`compilerOptions`](https://github.com/TypeStrong/atom-typescript/blob/e2fa67c4715189b71430f766ed9a92d9fb3255f9/lib/main/tsconfig/tsconfig.ts#L8-L35): similar to what you would pass on the commandline to `tsc`.
1414
* One exception : [We don't support `--out` because it will hurt you in the long run, and we will warn you if you use it](https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md).
1515
* [`filesGlob`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#filesglob): To make it easier for you to just add / remove files in your project we add `filesGlob` which accepts an array of `glob / minimatch / RegExp` patterns (similar to grunt) to specify source files.
16-
* [`package`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#package): The path to a package.json. We can use this to generate a *.d.ts* file for your project using `name`, `main` and `typescript.definition`.
1716
* [`formatCodeOptions`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#formatcodeoptions) : Code formatting options
1817
* [`compileOnSave`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#compileonsave) : Should AtomTS compile on save
1918
* [`version`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#version): The TypeScript version
@@ -50,29 +49,6 @@ Note: `files` is kept up to date by expansion of `filesGlob`.
5049
}
5150
```
5251

53-
### package
54-
55-
```json
56-
{
57-
"package": "./package.json"
58-
}
59-
```
60-
61-
Where a sample package.json looks like:
62-
```json
63-
{
64-
"name": "awesome",
65-
"main": "./dist/foo.js",
66-
"typescript": {
67-
"definition": "./definition/awesome.d.ts"
68-
}
69-
}
70-
```
71-
72-
We would generate a `definition/awesome.d.ts` file for you so that other TypeScript projects can do a simple `require('awesome')`.
73-
74-
Also note that any node modules that ship with `typescript.definition` that you import will automatically get type inference / completion.
75-
7652
### formatCodeOptions
7753
These are used when you request the IDE to format TypeScript code.
7854

lib/main/tsconfig/tsconfig.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ interface TypeScriptProjectRawSpecification {
7676
filesGlob?: string[]; // optional: An array of 'glob / minimatch / RegExp' patterns to specify source files
7777
formatCodeOptions?: formatting.FormatCodeOptions; // optional: formatting options
7878
compileOnSave?: boolean; // optional: compile on save. Ignored to build tools. Used by IDEs
79-
package?: string; // optional: Path to your package.json. If you specify this we can do some cool stuff like generate a .d.ts for you.
8079
}
8180

8281
interface UsefulFromPackageJson {
@@ -345,17 +344,22 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
345344
// Remove all relativeness
346345
projectSpec.files = projectSpec.files.map((file) => path.resolve(projectFileDirectory, file));
347346

348-
var packagePath = projectSpec.package;
349347
var package: UsefulFromPackageJson = null;
350-
if (packagePath) {
351-
let packageJSONPath = getPotentiallyRelativeFile(projectFileDirectory, packagePath);
352-
let parsedPackage = JSON.parse(fs.readFileSync(packageJSONPath).toString());
353-
package = {
354-
main: parsedPackage.main,
355-
name: parsedPackage.name,
356-
directory: path.dirname(packageJSONPath),
357-
definition: parsedPackage.typescript && parsedPackage.typescript.definition
358-
};
348+
try {
349+
var packagePath = travelUpTheDirectoryTreeTillYouFind(projectFileDirectory, 'package.json');
350+
if (packagePath) {
351+
let packageJSONPath = getPotentiallyRelativeFile(projectFileDirectory, packagePath);
352+
let parsedPackage = JSON.parse(fs.readFileSync(packageJSONPath).toString());
353+
package = {
354+
main: parsedPackage.main,
355+
name: parsedPackage.name,
356+
directory: path.dirname(packageJSONPath),
357+
definition: parsedPackage.typescript && parsedPackage.typescript.definition
358+
};
359+
}
360+
}
361+
catch (ex) {
362+
// Don't care :)
359363
}
360364

361365
var project: TypeScriptProjectSpecification = {
@@ -604,7 +608,9 @@ export function removeTrailingSlash(filePath: string) {
604608
return filePath;
605609
}
606610

607-
/** returns the path if found or throws an error "not found" if not found */
611+
/**
612+
* returns the path if found
613+
* @throws an error "not found" if not found */
608614
export function travelUpTheDirectoryTreeTillYouFind(dir: string, fileOrDirectory: string,
609615
/** This is useful if we don't want to file `node_modules from inside node_modules` */
610616
abortIfInside = false): string {

0 commit comments

Comments
 (0)