Skip to content

Commit

Permalink
Add kotlin tests and use tmpdir
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Dec 18, 2018
1 parent 2b93919 commit 5e4fbd9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/core/fs/package.json
Expand Up @@ -20,7 +20,8 @@
"prepublish": "yarn build"
},
"dependencies": {
"@parcel/utils": "^1.10.3",
"mkdirp": "^0.5.1",
"@parcel/utils": "^1.10.3"
"rimraf": "^2.6.2"
}
}
2 changes: 2 additions & 0 deletions packages/core/fs/src/fs.js
@@ -1,12 +1,14 @@
const {promisify} = require('@parcel/utils');
const fs = require('fs');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');

exports.readFile = promisify(fs.readFile);
exports.writeFile = promisify(fs.writeFile);
exports.stat = promisify(fs.stat);
exports.readdir = promisify(fs.readdir);
exports.unlink = promisify(fs.unlink);
exports.rimraf = promisify(rimraf);
exports.realpath = async function(path) {
const realpath = promisify(fs.realpath);
try {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/integration-tests/package.json
Expand Up @@ -13,12 +13,14 @@
},
"devDependencies": {
"@babel/core": "^7.2.0",
"@jetbrains/kotlinc-js-api": "^1.2.12",
"@parcel/fs": "^1.10.3",
"@parcel/test-utils": "^1.10.3",
"codecov": "^3.0.0",
"command-exists": "^1.2.6",
"graphql-tag": "^2.6.0",
"json5": "^1.0.1",
"kotlin": "^1.3.11",
"mocha": "^5.1.1",
"mocha-junit-reporter": "^1.18.0",
"mocha-multi-reporters": "^1.1.7",
Expand Down
@@ -0,0 +1,3 @@
var test = require('./test.kt');

module.exports = test.sum(2, 3);
@@ -0,0 +1,4 @@
@JsName("sum")
fun sum(a: Int, b: Int): Int {
return a + b
}
17 changes: 17 additions & 0 deletions packages/core/integration-tests/test/kotlin.js
@@ -0,0 +1,17 @@
const assert = require('assert');
const fs = require('@parcel/fs');
const {bundle, assertBundleTree, run} = require('./utils');

describe('kotlin', function() {
it('should produce a basic kotlin bundle', async function() {
let b = await bundle(__dirname + '/integration/kotlin/index.js');

await assertBundleTree(b, {
type: 'js',
assets: ['test.kt', 'index.js', 'browser.js', 'kotlin.js']
});

let output = await run(b);
assert.equal(output, 5);
});
});
35 changes: 17 additions & 18 deletions packages/core/parcel-bundler/src/assets/KotlinAsset.js
@@ -1,5 +1,9 @@
const Asset = require('../Asset');
const localRequire = require('../utils/localRequire');
const path = require('path');
const fs = require('@parcel/fs');
const os = require('os');
const spawn = require('cross-spawn');

class KotlinAsset extends Asset {
constructor(name, options) {
Expand All @@ -13,29 +17,28 @@ class KotlinAsset extends Asset {
'@jetbrains/kotlinc-js-api',
this.name
);
const fs = require('fs', this.name);

// remove extension and path
let slashyIndex = this.id.lastIndexOf('/'); // linux/mac
if (slashyIndex === -1) slashyIndex = this.id.lastIndexOf('\\'); // windows
const fileName = this.id.substring(slashyIndex, this.id.lastIndexOf('.'));
let id = Math.random()
.toString(36)
.slice(3);
let dir = path.join(os.tmpdir(), id);
let filename = path.join(dir, id + '.js');

await fs.mkdirp(dir);

await kotlinCompiler.compile({
output: 'build/kt.temp/' + fileName + '.js',
output: filename,
sources: [this.name],
moduleKind: 'commonjs',
noStdlib: false,
metaInfo: false,
metaInfo: true,
sourceMaps: this.options.sourceMaps
});

let source = fs.readFileSync('build/kt.temp/' + fileName + '.js', 'utf8');
let source = await fs.readFile(filename, 'utf8');
let sourceMap;
if (this.options.sourceMaps) {
sourceMap = fs.readFileSync(
'build/kt.temp/' + fileName + '.js.map',
'utf8'
);
sourceMap = await fs.readFile(filename + '.map', 'utf8');

sourceMap = JSON.parse(sourceMap);
sourceMap.sources = [this.relativeName];
Expand All @@ -45,12 +48,8 @@ class KotlinAsset extends Asset {
source = source.substring(0, source.lastIndexOf('//# sourceMappingURL'));
}

// delete old files
fs.unlinkSync('build/kt.temp/' + fileName + '.js');
fs.unlinkSync('build/kt.temp/' + fileName + '.js.map');
if (fs.readdirSync('build/kt.temp').length === 0)
fs.rmdirSync('build/kt.temp'); // Remove kt.temp directory if it is empty
if (fs.readdirSync('build').length === 0) fs.rmdirSync('build'); // Remove build directory if it is empty
// delete temp directory
await fs.rimraf(dir);

return [
{
Expand Down

0 comments on commit 5e4fbd9

Please sign in to comment.