Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 30654bc

Browse files
committed
revert #3
1 parent 0aa6ae7 commit 30654bc

File tree

9 files changed

+364
-89
lines changed

9 files changed

+364
-89
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
![beta](https://img.shields.io/badge/build-beta-yellow.svg?style=flat)
2+
13
#### atom-import-cost
24

35
 
@@ -35,3 +37,5 @@ Please feel free to help out with these :)
3537
:star: this repo
3638

3739
 
40+
41+
[![Sponsor](https://app.codesponsor.io/embed/LhLT2c31ydJzdLUuSR9f8mCA/siddharthkp/atom-import-cost.svg)](https://app.codesponsor.io/link/LhLT2c31ydJzdLUuSR9f8mCA/siddharthkp/atom-import-cost)

lib/imports.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use babel'
2+
3+
import readPkgUp from 'read-pkg-up'
4+
import Walker from 'node-source-walk'
5+
6+
const isValidModule = ({ value }) => {
7+
const regex = new RegExp('^([a-z0-9-_]{1,})$')
8+
return regex.test(value)
9+
}
10+
11+
module.exports = src => {
12+
const walker = new Walker()
13+
const dependencies = []
14+
15+
if (src === '') return dependencies
16+
17+
try {
18+
walker.walk(src, function(node) {
19+
switch (node.type) {
20+
case 'ImportDeclaration':
21+
if (node.source && node.source.value) {
22+
dependencies.push(node.source)
23+
}
24+
break
25+
case 'ExportNamedDeclaration':
26+
case 'ExportAllDeclaration':
27+
if (node.source && node.source.value) {
28+
dependencies.push(node.source)
29+
}
30+
break
31+
default:
32+
return
33+
}
34+
})
35+
} catch (e) {}
36+
37+
return dependencies.filter(isValidModule)
38+
}

lib/main.js

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,69 @@
11
'use babel'
22

3-
import { importCost, cleanup } from 'import-cost';
4-
import Utils from './utils'
5-
6-
let emitters = {};
7-
let markers = {};
3+
import { Point } from 'atom'
4+
import imports from './imports'
5+
import getSize from './size'
86

97
export default {
8+
subscriptions: null,
9+
decorations: [],
10+
1011
activate() {
11-
atom.workspace.onDidChangeActivePaneItem(() => this.calculateImportCost())
12+
console.log('import cost activated')
13+
this.attachLabels()
14+
15+
atom.workspace.onDidChangeActivePaneItem(() => this.attachLabels())
1216

1317
atom.workspace.observeTextEditors(editor =>
14-
editor.onDidSave(() => this.calculateImportCost())
18+
editor.onDidSave(() => this.attachLabels())
1519
)
20+
},
1621

17-
this.calculateImportCost()
22+
removeLabels() {
23+
this.decorations.map(d => d.destroy())
24+
this.decorations = []
1825
},
1926

20-
calculateImportCost() {
21-
// Get the active editor
27+
attachLabels() {
28+
this.removeLabels()
2229
const editor = atom.workspace.getActiveTextEditor()
2330
if (!editor) return
2431

25-
// Get the current file path and check whether it is a valid file extension
26-
const filePath = editor.getPath();
27-
const fileExtension = Utils.extractFileExtension(filePath);
28-
if (!fileExtension) return
32+
const code = editor.getText()
2933

30-
// Get the content of the active editor
31-
const content = editor.getText()
34+
const dependencies = imports(code)
3235

33-
// Remove active importCost event listeners
34-
if (emitters[filePath]) {
35-
emitters[filePath].removeAllListeners()
36-
}
36+
dependencies.map(d => {
37+
const name = d.value
3738

38-
// Calculate import cost of the active editor
39-
emitters[filePath] = importCost(filePath, content, fileExtension);
40-
emitters[filePath].on('calculated', packageInfo => this.renderImportCost(editor, packageInfo))
41-
},
39+
const line = d.loc.start.line - 1 // starts at 0 in atom
40+
const length = editor.lineLengthForScreenRow(line)
4241

43-
clearImportCost(packageName) {
44-
let marker = markers[packageName]
45-
if (marker) {
46-
marker.destroy()
47-
delete markers[packageName]
48-
}
49-
},
42+
const label = document.createElement('span')
43+
label.className = 'atom-import-cost-label'
44+
label.textContent = '...'
45+
46+
const position = new Point(line, length)
5047

51-
renderImportCost(editor, packageInfo) {
52-
// Remove labels
53-
this.clearImportCost(packageInfo.name)
48+
const marker = editor.markScreenPosition(position)
49+
const decoration = editor.decorateMarker(marker, {
50+
type: 'overlay',
51+
item: label
52+
})
5453

55-
// Get the position where we are going to insert the label
56-
const row = packageInfo.line - 1
57-
const col = editor.lineLengthForScreenRow(row)
54+
getSize(name).then(size => {
55+
if (size === 'invalid') label.textContent = ''
56+
else if (size) label.textContent = size
57+
else label.textContent = '¯\\_(ツ)_/¯ could not fetch size, API is down!'
5858

59-
// Create a label and render it
60-
markers[packageInfo.name] = Utils.createMarker(packageInfo.size, editor, row, col)
59+
decoration.setProperties({ item: label })
60+
})
61+
62+
this.decorations.push(decoration)
63+
})
6164
},
6265

6366
deactivate() {
64-
cleanup()
65-
66-
for (var emitter in emitters) {
67-
if (emitters.hasOwnProperty(emitter)) {
68-
emitters[emitter].removeAllListeners()
69-
delete emitters[emitter]
70-
}
71-
}
72-
73-
for (var marker in markers) {
74-
if (markers.hasOwnProperty(marker)) {
75-
markers[marker].destroy()
76-
delete markers[marker]
77-
}
78-
}
79-
67+
this.subscriptions.dispose()
8068
}
8169
}

lib/size.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use babel'
2+
3+
import axios from 'axios'
4+
import bytes from 'bytes'
5+
import versions from './versions'
6+
7+
const cache = {}
8+
9+
module.exports = name => {
10+
return new Promise(resolve => {
11+
if (versions(name)) name += '@' + versions(name)
12+
13+
if (cache[name]) {
14+
if (cache[name] === 'invalid') resolve('invalid')
15+
else resolve(bytes(cache[name]))
16+
} else {
17+
axios
18+
.get('https://cost-of-modules.herokuapp.com/package?name=' + name)
19+
.then(result => {
20+
const size = result.data.gzipSize
21+
cache[name] = size
22+
resolve(bytes(size))
23+
})
24+
.catch(error => {
25+
if (error.response && error.response.status === 400) {
26+
cache[name] = 'invalid'
27+
resolve('invalid')
28+
} else resolve('error')
29+
})
30+
}
31+
})
32+
}

lib/utils.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/versions.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use babel'
2+
3+
import readPkgUp from 'read-pkg-up'
4+
5+
module.exports = name => {
6+
let dependencies = {}
7+
8+
try {
9+
const directory = atom.workspace.getActiveTextEditor().getDirectoryPath()
10+
dependencies = readPkgUp.sync({ cwd: directory }).pkg.dependencies
11+
} catch (error) {}
12+
13+
if (!dependencies) dependencies = {}
14+
15+
return dependencies[name] || null
16+
}

0 commit comments

Comments
 (0)