Merge branch 'master' into revert-787-remove_npm_bits
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b0832bf..178fd34 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+# 1.3.2
+
+* Restore the code that publishes the dart-style npm package.
+
# 1.3.1
* Fix crash in formatting complex method chains (#855).
diff --git a/pubspec.lock b/pubspec.lock
index a2c3dca..7c2c702 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -23,7 +23,7 @@
source: hosted
version: "1.5.2"
async:
- dependency: transitive
+ dependency: "direct dev"
description:
name: async
url: "https://pub.dartlang.org"
@@ -135,7 +135,7 @@
source: hosted
version: "0.3.3"
js:
- dependency: transitive
+ dependency: "direct dev"
description:
name: js
url: "https://pub.dartlang.org"
@@ -177,7 +177,7 @@
source: hosted
version: "1.0.2"
node_preamble:
- dependency: transitive
+ dependency: "direct dev"
description:
name: node_preamble
url: "https://pub.dartlang.org"
diff --git a/pubspec.yaml b/pubspec.yaml
index 990ce7e..50c1044 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,6 +1,6 @@
name: dart_style
# Note: See tool/grind.dart for how to bump the version.
-version: 1.3.1
+version: 1.3.2-dev
author: Dart Team <misc@dartlang.org>
description: >-
Opinionated, automatic Dart source code formatter.
@@ -17,7 +17,10 @@
source_span: ^1.4.0
dev_dependencies:
+ async: '>=1.0.0 <=3.0.0'
grinder: ^0.8.0
+ js: ^0.6.0
+ node_preamble: ^1.0.0
pedantic: ^1.0.0
pub_semver: ^1.2.3
test: ^1.6.0
diff --git a/tool/grind.dart b/tool/grind.dart
index d5d794d..5810053 100644
--- a/tool/grind.dart
+++ b/tool/grind.dart
@@ -2,7 +2,11 @@
// 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:node_preamble/preamble.dart" as preamble;
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart' as yaml;
@@ -24,6 +28,41 @@
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 = File('${Directory.systemTemp.path}/temp.js');
+
+ Dart2js.compile(File('tool/node_format_service.dart'),
+ outFile: tempFile, categories: 'all');
+ var dart2jsOutput = tempFile.readAsStringSync();
+ File('$out/$fileName').writeAsStringSync('''${preamble.getPreamble()}
+self.exports = exports; // Temporary hack for Dart-JS Interop under node.
+$dart2jsOutput''');
+
+ 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:
diff --git a/tool/node_format_service.dart b/tool/node_format_service.dart
new file mode 100644
index 0000000..cfc5cf1
--- /dev/null
+++ b/tool/node_format_service.dart
@@ -0,0 +1,55 @@
+// 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 'dart:math' as math;
+
+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 = DartFormatter();
+
+ var exception;
+ try {
+ return FormatResult(code: DartFormatter().format(source));
+ } on FormatterException catch (err) {
+ // Couldn't parse it as a compilation unit.
+ exception = err;
+ }
+
+ // Maybe it's a statement.
+ try {
+ return FormatResult(code: formatter.formatStatement(source));
+ } on FormatterException catch (err) {
+ // There is an error when parsing it both as a compilation unit and a
+ // statement, so we aren't sure which one the user intended. As a
+ // heuristic, we'll choose that whichever one we managed to parse more of
+ // before hitting an error is probably the right one.
+ if (_firstOffset(exception) < _firstOffset(err)) {
+ exception = err;
+ }
+ }
+
+ // If we get here, it couldn't be parsed at all.
+ return FormatResult(code: source, error: "$exception");
+ });
+}
+
+/// Returns the offset of the error nearest the beginning of the file out of
+/// all the errors in [exception].
+int _firstOffset(FormatterException exception) =>
+ exception.errors.map((error) => error.offset).reduce(math.min);