Skip to content

Commit

Permalink
Support deploying an npm package exporting a formatCode method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob314 committed Jan 28, 2016
1 parent 656695e commit fd89540
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.2.6

* Support deploying an npm package exporting a formatCode method.

# 0.2.4

* Better handling for long collections with comments (#484).
Expand Down
8 changes: 8 additions & 0 deletions dist/dart-style.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Type definitions for dart-style

interface FormatResult {
code?: string;
error?: string;
}

export function formatCode(code: string): FormatResult;
50 changes: 25 additions & 25 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ packages:
analyzer:
description: analyzer
source: hosted
version: "0.27.0"
version: "0.27.1+2"
ansicolor:
description: ansicolor
source: hosted
version: "0.0.9"
args:
description: args
source: hosted
version: "0.13.2"
version: "0.13.3+1"
async:
description: async
source: hosted
version: "1.4.0"
version: "1.8.0"
barback:
description: barback
source: hosted
Expand All @@ -36,11 +36,7 @@ packages:
collection:
description: collection
source: hosted
version: "1.2.0"
convert:
description: convert
source: hosted
version: "1.0.1"
version: "1.3.0"
crypto:
description: crypto
source: hosted
Expand All @@ -52,27 +48,31 @@ packages:
glob:
description: glob
source: hosted
version: "1.0.5"
version: "1.1.0"
grinder:
description: grinder
source: hosted
version: "0.7.2"
version: "0.8.0+2"
html:
description: html
source: hosted
version: "0.12.2"
version: "0.12.2+1"
http:
description: http
source: hosted
version: "0.11.3+2"
version: "0.11.3+3"
http_multi_server:
description: http_multi_server
source: hosted
version: "2.0.0"
http_parser:
description: http_parser
source: hosted
version: "1.1.0"
version: "2.0.0"
js:
description: js
source: hosted
version: "0.6.0"
logging:
description: logging
source: hosted
Expand All @@ -89,6 +89,10 @@ packages:
description: mockable_filesystem
source: hosted
version: "0.0.3"
node_preamble:
description: node_preamble
source: hosted
version: "1.0.0"
package_config:
description: package_config
source: hosted
Expand All @@ -112,19 +116,19 @@ packages:
scheduled_test:
description: scheduled_test
source: hosted
version: "0.12.4+2"
version: "0.12.4+4"
shelf:
description: shelf
source: hosted
version: "0.6.4+2"
version: "0.6.4+3"
shelf_static:
description: shelf_static
source: hosted
version: "0.2.3+1"
version: "0.2.3+2"
shelf_web_socket:
description: shelf_web_socket
source: hosted
version: "0.0.1+4"
version: "0.0.1+5"
source_map_stack_trace:
description: source_map_stack_trace
source: hosted
Expand All @@ -140,23 +144,19 @@ packages:
stack_trace:
description: stack_trace
source: hosted
version: "1.5.0"
version: "1.6.0"
string_scanner:
description: string_scanner
source: hosted
version: "0.1.4"
version: "0.1.4+1"
supports_color:
description: supports_color
source: hosted
version: "0.1.1"
test:
description: test
source: hosted
version: "0.12.6+1"
typed_data:
description: typed_data
source: hosted
version: "1.1.1"
version: "0.12.8"
unscripted:
description: unscripted
source: hosted
Expand All @@ -180,4 +180,4 @@ packages:
yaml:
description: yaml
source: hosted
version: "2.1.7"
version: "2.1.8"
7 changes: 5 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dart_style
version: 0.2.4
version: 0.2.6
author: Dart Team <[email protected]>
description: Opinionated, automatic Dart source code formatter.
homepage: https://github.com/dart-lang/dart_style
Expand All @@ -10,10 +10,13 @@ dependencies:
args: '>=0.12.1 <0.14.0'
path: '>=1.0.0 <2.0.0'
source_span: '>=1.1.1 <2.0.0'

dev_dependencies:
async: '>=1.0.0 <=2.0.0'
browser: '>=0.10.0 <0.11.0'
grinder: '^0.7.2'
grinder: '^0.8.0'
js: ^0.6.0
node_preamble: ^1.0.0
pub_semver: '^1.2.3'
scheduled_test: '>=0.12.0 <0.13.0'
test: '>=0.12.0 <0.13.0'
Expand Down
40 changes: 40 additions & 0 deletions tool/grind.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:convert';
import 'dart:io';

import 'package:grinder/grinder.dart';
import 'package:grinder/src/run.dart' as runner;
import "package:node_preamble/preamble.dart" as preamble;
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart' as yaml;

Expand All @@ -24,6 +29,41 @@ validate() async {
Dart.run("bin/format.dart", arguments: ["-w", "."]);
}

@Task('Publish to npm')
npm() {
var out = 'dist';

var pubspec = yaml.loadYaml(getFile("pubspec.yaml").readAsStringSync());
var homepage = pubspec["homepage"];
var fileName = 'index.js';

// Generate modified dart2js output suitable to run on node.
var tempFile = new File('${Directory.systemTemp.path}/temp.js');

Dart2js.compile(new File('tool/node_format_service.dart'),
outFile: tempFile, categories: 'all');
var dart2jsOutput = tempFile.readAsStringSync();
new File('$out/$fileName').writeAsStringSync('''${preamble.getPreamble()}
self.exports = exports; // Temporary hack for Dart-JS Interop under node.
$dart2jsOutput''');

new File('$out/package.json')
.writeAsStringSync(const JsonEncoder.withIndent(' ').convert({
"name": "dart-style",
"version": pubspec["version"],
"description": pubspec["description"],
"main": fileName,
"typings": "dart-style.d.ts",
"scripts": {"test": "echo \"Error: no test specified\" && exit 1"},
"repository": {"type": "git", "url": "git+$homepage"},
"author": pubspec["author"],
"license": "BSD",
"bugs": {"url": "$homepage/issues"},
"homepage": homepage
}));
run('npm', arguments: ['publish', out]);
}

/// Gets ready to publish a new version of the package.
///
/// To publish a version, you need to:
Expand Down
38 changes: 38 additions & 0 deletions tool/node_format_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:js/js.dart';

import 'package:dart_style/dart_style.dart';

@JS()
@anonymous
class FormatResult {
external factory FormatResult({String code, String error});
external String get code;
external String get error;
}

@JS('exports.formatCode')
external set formatCode(Function formatter);

void main() {
formatCode = allowInterop((String source) {
var formatter = new DartFormatter();
try {
return new FormatResult(
code: new DartFormatter().format(source));
} on FormatterException {
// Do nothing.
}

// Maybe it's a statement.
try {
return new FormatResult(
code: formatter.formatStatement(source));
} on FormatterException catch (err) {
return new FormatResult(code: source, error: "$err");
}
});
}

0 comments on commit fd89540

Please sign in to comment.